Submission #217494

#TimeUsernameProblemLanguageResultExecution timeMemory
217494AutoratchRace (IOI11_race)C++14
21 / 100
3089 ms211192 KiB
#include "race.h" #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1; const int K = 20; int n,k,cn; vector<pair<int,int> > adj[N]; vector<int> cen[N]; int sz[N],pa[N],lv[N],dp[K][N],ans = INT_MAX; bool blocked[N]; long long dist[N]; unordered_map<int,int> res[N],lc[N]; void dfs(int u,int p){ sz[u] = 1; for(auto [d,v] : adj[u]) if(v!=p and !blocked[v]) dfs(v,u),sz[u]+=sz[v]; } void build(int u,int cp) { dfs(u,0); int c = u,prev = 0; while(true) { int mx = 0,id = 0; for(auto [d,v] : adj[c]) if(!blocked[v] and v!=prev) if(sz[v]>mx) mx = sz[v],id = v; if(mx*2>sz[u]) prev = c,c = id; else break; } if(cp==0) cn = c; pa[c] = cp; if(cp) cen[cp].push_back(c); blocked[c] = true; for(auto [d,v] : adj[c]) if(!blocked[v]) build(v,c); } void dfslca(int u,int p){ lv[u] = lv[p]+1,dp[0][u] = p; for(auto [d,v] : adj[u]) if(v!=p) dist[v] = dist[u]+(long long)d,dfslca(v,u); } int lca(int a,int b) { if(lv[a]<lv[b]) swap(a,b); for(int i = K-1;i >= 0;i--) if(lv[dp[i][a]]>=lv[b]) a = dp[i][a]; if(a==b) return a; for(int i = K-1;i >= 0;i--) if(dp[i][a]!=dp[i][b]) a = dp[i][a],b = dp[i][b]; return dp[0][a]; } void upd(int u,int p,bool f) { int l; if(lc[u][p]) l = lc[u][p]; else l = lc[u][p] = lca(u,p); long long all = dist[u]-dist[l]+dist[p]-dist[l]; if(all>k) return; int len = lv[u]-lv[l]+lv[p]-lv[l]; if(!f) { if(all==k) ans = min(ans,len); else if(res[p][k-all]) ans = min(ans,res[p][k-all]+len); } else if(res[p][all]==0 or res[p][all]>len) res[p][all] = len; } void sol(int c,int u,int t) { upd(u,c,t); for(int v : cen[u]) sol(c,v,t); } void run(int u) { for(int v : cen[u]) { sol(u,v,0); sol(u,v,1); } for(int v : cen[u]) run(v); } int best_path(int nn,int kk,int h[][2],int l[]) { n = nn,k = kk; for(int i = 0;i < n-1;i++) { int a = h[i][0],b = h[i][1],d = l[i]; a++,b++; adj[a].push_back({d,b}); adj[b].push_back({d,a}); } build(1,0); dfslca(1,0); for(int i = 1;i < K;i++) for(int j = 1;j <= n;j++) dp[i][j] = dp[i-1][dp[i-1][j]]; run(cn); if(ans==INT_MAX) ans = -1; return ans; }

Compilation message (stderr)

race.cpp: In function 'void dfs(int, int)':
race.cpp:16:44: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
 void dfs(int u,int p){ sz[u] = 1; for(auto [d,v] : adj[u]) if(v!=p and !blocked[v]) dfs(v,u),sz[u]+=sz[v]; }
                                            ^
race.cpp:16:48: warning: unused variable 'd' [-Wunused-variable]
 void dfs(int u,int p){ sz[u] = 1; for(auto [d,v] : adj[u]) if(v!=p and !blocked[v]) dfs(v,u),sz[u]+=sz[v]; }
                                                ^
race.cpp: In function 'void build(int, int)':
race.cpp:25:18: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
         for(auto [d,v] : adj[c]) if(!blocked[v] and v!=prev) if(sz[v]>mx) mx = sz[v],id = v;
                  ^
race.cpp:25:22: warning: unused variable 'd' [-Wunused-variable]
         for(auto [d,v] : adj[c]) if(!blocked[v] and v!=prev) if(sz[v]>mx) mx = sz[v],id = v;
                      ^
race.cpp:33:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
     for(auto [d,v] : adj[c]) if(!blocked[v]) build(v,c);
              ^
race.cpp:33:18: warning: unused variable 'd' [-Wunused-variable]
     for(auto [d,v] : adj[c]) if(!blocked[v]) build(v,c);
                  ^
race.cpp: In function 'void dfslca(int, int)':
race.cpp:36:66: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
 void dfslca(int u,int p){ lv[u] = lv[p]+1,dp[0][u] = p; for(auto [d,v] : adj[u]) if(v!=p) dist[v] = dist[u]+(long long)d,dfslca(v,u); }
                                                                  ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...