Submission #412554

#TimeUsernameProblemLanguageResultExecution timeMemory
412554jeqchoRace (IOI11_race)C++17
0 / 100
7 ms11212 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<pair<int,int>> vpi; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) #define pb push_back #define rsz resize #define sz(x) int(x.size()) #define all(x) begin(x), end(x) #define fi first #define se second // subtask 1,2,3 int const n=2e5; unordered_map<int,int> adj[n]; int sub[n]; int const INF=1e9; int mn=INF; int k; int getsub(int now, int par) { sub[now]=1; trav(chi,adj[now]) { if(chi.fi==par)continue; sub[now]+=getsub(chi.fi,now); } return sub[now]; } int centroid(int now, int par, int siz) { trav(chi,adj[now]) { if(chi.fi==par)continue; if(sub[chi.fi]>siz/2)return centroid(chi.fi,now,siz); } return now; } int const k_=1e6+3; pair<pii,pii> path[k_]; vi pos; bitset<k_> got; void dfs(int now, int par, int cur, int id, int dep) { if(cur>k)return; if(!got[cur]) { pos.pb(cur); got[cur]=1; path[cur] = {{INF,-1},{INF,-1}}; } if(path[cur].fi.fi==id) { path[cur].fi.se = min(path[cur].fi.se,dep); } else if(path[cur].se.fi==id) { path[cur].se.se = min(path[cur].se.se,dep); if(path[cur].se.se < path[cur].fi.se) { swap(path[cur].se.se, path[cur].fi.se); } } else if(dep < path[cur].fi.se) { path[cur].se = path[cur].fi; path[cur].fi = {id,dep}; } else if(dep < path[cur].se.se) { path[cur].se = {id,dep}; } trav(chi,adj[now]) { if(chi.fi==par)continue; dfs(chi.fi,now,cur+chi.se,id,dep+1); } } void solve(int now) { pos.clear(); trav(chi,adj[now]) { dfs(chi.fi,now,chi.se,chi.fi,1); } pos.pb(0); path[0].fi={0,now}; got[0]=1; trav(i,pos) { if(i>k/2)break; if(!got[k-i])continue; if(path[i].fi.fi==path[k-i].fi.fi) { if(path[i].se.fi!=-1) { mn=min(mn,path[i].se.se+path[k-i].fi.se); } if(path[i].fi.fi!=-1) { mn=min(mn,path[i].fi.se+path[k-i].se.se); } } else { mn=min(mn,path[i].fi.se+path[k-i].fi.se); } } trav(e,pos) { got[e]=0; } } void decomp(int now) { int siz = getsub(now,-1); int u = centroid(now,-1,siz); solve(u); trav(chi,adj[u]) { adj[chi.fi].erase(u); decomp(chi.fi); } } int best_path(int N, int K, int H[][2], int L[]){ k=K; F0R(i,N-1) { int u = H[i][0]; int v = H[i][1]; adj[u].insert({v,L[i]}); adj[v].insert({u,L[i]}); } decomp(0); if(mn==INF)return -1; return mn; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...