# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1007477 | 2024-06-25T03:44:33 Z | ezzzay | Race (IOI11_race) | C++14 | 0 ms | 0 KB |
//#include "race.h" #include<bits/stdc++.h> using namespace std; const int M=2e5+5; long long ans=INT_MAX; vector<pair<int,int>>v[M]; #define pb push_back #define ll long long #define ff first #define ss second int sum[M],dst[M]; map<ll,ll> info[M]; int k; void dfs(int a, int p, int h, ll d){ info[a][d]=h; dst[a]=h; sum[a]=d; info[a][d]=h; for(auto b:v[a]){ if(b.ff==p)continue; dfs(b,a,h+1,d+b.ss); } } void fun(int a, int p){ ll t=k+2*sum[a]; for(auto b:v[a]){ if(b.ff==p)continue; fun(b.ff,a); if(info[b.ff].size()>info[a].size())swap(info[b.ff],info[a]); for(auto i:info[b.ff]){ if(info[a].find(t-i.ff)!=info[a].end()){ ans=min(ans,info[a][t-i.ff]+i.ss-2*dst[a]); } } for (auto i : info[b.ff]) { if (info[a].find(i.ff) == info[a].end()) { info[a].insert(i); } else { info[a][i.ff] = min(info[a][i.ff], i.ss); } } } } int best_path(int N, int K, int H[][2], int L[]){ k=K; if(k==1)return 0; for(int i=0;i<N-1;i++){ int a=H[i][0]; int b=H[i][1]; int c=L[i]; v[a].pb({b,c}); v[b].pb({a,c}); } dfs(0,-1,0,0); fun(0,-1); return ans==INT_MAX ? -1 : ans; }