This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
/*------------------------------------code------------------------------------*/
const ll MAXN=1e5+9;
const ll INF=1e9+9;
const ll R=(1<<18);
const int X=18;
//const ll M=1e9+7;
//const ll P=47;
int n, k;
vector<pair<int, int>> adj[MAXN];
int ans=INF;
void dfs(int v, int p, ll d, int cnt)
{
if(d==k)
{
ans=min(ans, cnt);
return;
}
for(auto [u, w] : adj[v])
{
if(u==p || d+w>k || cnt+1>ans)
continue;
dfs(u, v, d+w, cnt+1);
}
}
int best_path(int N, int K, int edges[][2], int weights[])
{
if(k==1)
return 0;
n=N;
k=K;
for(int i=0;i<n-1;i++)
{
int u = edges[i][0];
int v = edges[i][1];
adj[u].pb({v, weights[i]});
adj[v].pb({u, weights[i]});
}
for(int i=1;i<=n;i++)
dfs(i, -1, 0, 0);
return (ans==INF ? -1 : ans);
}
/*
int main()
{
cin.tie(0); ios_base::sync_with_stdio(0);
solve();
return 0;
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |