Submission #700869

#TimeUsernameProblemLanguageResultExecution timeMemory
700869kusmetliqRace (IOI11_race)C++14
43 / 100
3043 ms60080 KiB
#include<bits/stdc++.h> using namespace std; const int maxN=200005; const long long big=100000000000000; long long k; long long minans=INT_MAX; vector<int>a[maxN]; map<pair<int,int>,int>dist; long long path[1000001]; bool used[maxN]; int sz[maxN]; long long min(long long a,long long b) { return (a<b?a:b); } int upd_sz(int n,int parent=-1) { sz[n]=1; for(int i:a[n]) { if(i!=parent && used[i]==0)sz[n]+=upd_sz(i,n); } return sz[n]; } int find_cen(int n,int siz,int parent=-1) { for(int i:a[n]) { if(used[i]==0 && sz[i]>siz/2 && i!=parent)return find_cen(i,siz,n); } return n; } void upd_ans(int n,int depth,long long distance,int parent=-1) { if(k-distance>=0)minans=min(minans,depth+path[k-distance]+big); for(int i:a[n]) { if(used[i]==0 && i!=parent) { upd_ans(i,depth+1,distance+dist[{i,n}],n); } } } vector<int>vec; void upd_score(int n,int depth,long long distance,int parent=-1) { if(distance<=k){path[distance]=min(path[distance],depth-big);vec.push_back(distance);} for(int i:a[n]) { if(used[i]==0 && i!=parent) { upd_score(i,depth+1,distance+dist[{i,n}],n); } } } void dfs(int n) { int c=find_cen(n,upd_sz(n)); used[c]=1; path[0]=-big; vec.clear(); for(int i:a[c]) { if(used[i]==0) { upd_ans(i,1,dist[{c,i}]); upd_score(i,1,dist[{c,i}]); } } for(int i:vec)path[i]=0; for(int i:a[c]) { if(used[i]==0)dfs(i); } } int best_path(int N,int K,int H[][2],int L[]) { k=K; for(int i=0;i<N-1;i++) { a[H[i][0]].push_back(H[i][1]); a[H[i][1]].push_back(H[i][0]); dist[{H[i][0],H[i][1]}]=dist[{H[i][1],H[i][0]}]=L[i]; } dfs(0); if(minans==INT_MAX)return -1; else return minans; } /** int main() { int n; cin>>n>>k; for(int i=0;i<n-1;i++) { int z; int x,y;cin>>x>>y>>z; a[x].push_back(y); a[y].push_back(x); dist[{x,y}]=dist[{y,x}]=z; } dfs(0); if(minans==INT_MAX)cout<<-1<<endl; else cout<<minans<<endl; return 0; }*/ /** 4 3 0 1 1 1 2 2 1 3 4 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...