제출 #42431

#제출 시각아이디문제언어결과실행 시간메모리
42431comfile경주 (Race) (IOI11_race)C++11
43 / 100
3051 ms33276 KiB
#include <bits/stdc++.h> #define mp make_pair #define pb push_back using namespace std; const int MN = 200010; int n,k,ans,sz[MN],dead[MN]; vector<pair<int,int> > g[MN]; void get_sz(int cur,int prev){ sz[cur] = 1; for(auto &x : g[cur]) if(x.first!=prev && !dead[x.first]){ get_sz(x.first,cur); sz[cur] += sz[x.first]; } } int dfs(int cur,int prev,int tot){ for(auto &x : g[cur]) if(x.first!=prev && !dead[x.first]){ if(sz[x.first]>(tot/2)) return dfs(x.first,cur,tot); } return cur; } int one(int root){ get_sz(root,-1); return dfs(root,-1,sz[root]); } void rec(int start){ int c = one(start); dead[c] = 1; for(auto &x : g[c]) if(!dead[x.first]) rec(x.first); map<int,int> cnt; function<void (int,int,int,int,int)> add_cnt = [&](int cur,int prev,int d,int st,int add){ if(d>k) return; cnt[d] = cnt[d]?min(cnt[d],st):st; for(auto &x : g[cur]) if(x.first!=prev && !dead[x.first]){ add_cnt(x.first,cur,d+x.second,st+1,add); } }; function<void (int,int,int,int)> calc = [&](int cur,int prev,int d,int st){ if(d>k) return; if(k!=d && cnt[k-d]) ans = min(ans,st+cnt[k-d]); for(auto &x: g[cur]) if(x.first!=prev && !dead[x.first]){ calc(x.first,cur,d+x.second,st+1); } }; add_cnt(c,-1,0,0,1); if(cnt[k]) ans = min(ans,cnt[k]); cnt.clear(); for(auto &x : g[c]) if(!dead[x.first]){ calc(x.first,c,x.second,1); add_cnt(x.first,c,x.second,1,1); } dead[c] = 0; } int best_path(int _n,int _k,int h[][2],int l[]){ ans = MN; n = _n; k = _k; for(int i=0;i<n-1;i++){ int a = h[i][0]; int b = h[i][1]; int c = l[i]; g[a].pb(mp(b,c)); g[b].pb(mp(a,c)); } rec(0); if(ans>=MN) ans = -1; return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...