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>
#define pb push_back
#define f first
#define s second
using namespace std;
const int N=2e5+5;
int n,k,ans=1e9;
int depth[N];
long long d[N];
vector<pair<int,int>> g[N];
map<pair<int,int>,int> edge;
map<long long,long long> mymap[N];
void dfs(int x, int p) {
d[x]=d[p]+edge[{x,p}]; depth[x]=depth[p]+1;
mymap[x][d[x]]=depth[x];
for (auto A:g[x]) {
int y=A.f,w=A.s;
if (y==p) continue;
dfs(y,x);
int sx=mymap[x].size(),sy=mymap[y].size();
if (sx<sy) swap(mymap[x],mymap[y]);
for (auto AA:mymap[y]) {
long long D=AA.f;
long long DEPTH=AA.s;
if (mymap[x][k+2*d[x]-D]>0) {
int val=mymap[x][k+2*d[x]-D]+DEPTH-2*depth[x];
ans=min(ans,val);
}
}
for (auto AA:mymap[y]) {
long long D=AA.f;
long long DEPTH=AA.s;
if (D==0) continue;
if (mymap[x][D]==0) mymap[x][D]=DEPTH;
else mymap[x][D]=min(mymap[x][D],DEPTH);
}
}
// cout<<"X "<<x<<" "<<ans<<endl;
// for (auto A:mymap[x]) {
// cout<<A.f<<" "<<A.s<<endl;
// }
}
int best_path(int N, int K, int H[][2], int L[]) {
n=N; k=K; edge[{0,0}]=1;
for (int i=0; i<n-1; i++) {
int a=H[i][1],b=H[i][0],c=L[i];
g[a].pb({b,c}); g[b].pb({a,c});
edge[{a,b}]=edge[{b,a}]=c;
}
dfs(0,0);
if (ans==1000000000) ans=-1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'void dfs(int, int)':
race.cpp:20:19: warning: unused variable 'w' [-Wunused-variable]
20 | int y=A.f,w=A.s;
| ^
# | 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... |