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 "race.h"
#include <vector>
#include <algorithm>
using namespace std;
int best;
void dfs(int a,int par,vector<vector<pair<int,int>>>& g,int cnt,int dist,int k){
if(cnt>k||dist>=best)return;
if(cnt==k){
best=min(best,dist);
return;
}
for(auto x:g[a]){
if(x.second==par)continue;
dfs(x.second,a,g,cnt+x.first,dist+1,k);
}
}
int best_path(int N, int K, int H[][2], int L[]){
vector<vector<pair<int,int>>> g(N,vector<pair<int,int>>());
for(int i=0;i<N-1;i++){
g[H[i][0]].push_back({L[i],H[i][1]});
g[H[i][1]].push_back({L[i],H[i][0]});
}
for(int i=0;i<N;i++)sort(g[i].begin(),g[i].end());
best=1e9;
for(int i=0;i<N;i++)dfs(i,i,g,0,0,K);
if(best==1e9)best=-1;
return best;
}
# | 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... |