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 "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const ll NN=2e5+5;
vector<pair<int,int>> g[NN];
int ans=1e9,k;
map<int,int> mp[NN];
void dfs(int x, int p, int d, int depth){
mp[x][d]=depth;
for(auto it: g[x]){
if(it.fr==p) continue;
dfs(it.fr,x,d+it.sc,depth+1);
if(mp[x].size()<mp[it.fr].size())
swap(mp[x],mp[it.fr]);
for(auto i: mp[it.fr]){
ll target=k-i.fr+2*d;
if(mp[x].find(target)!=mp[x].end()){
ans=min(ans,mp[x][target]+i.sc-2*depth);
}
}
for(auto i: mp[it.fr]) mp[x].insert(i);
}
}
int best_path(int N, int K, int H[][2], int L[]){
k=K;
for(int i=0;i<N-1;i++){
int a=H[i][0],b=H[i][1],c=L[i];
g[a].pb({b,c});
g[b].pb({a,c});
}
dfs(0,-1,0,0);
if(ans>N) ans=-1;
return ans;
}
# | 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... |