Submission #412545

#TimeUsernameProblemLanguageResultExecution timeMemory
412545jeqchoRace (IOI11_race)C++17
43 / 100
3107 ms150900 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; unordered_set<int> path[k_]; unordered_set<int> pos; bitset<k_> got; unordered_map<int,int>len[n]; void dfs(int now, int par, int cur, int id, int dep) { if(cur>k)return; pos.insert(cur); got[cur]=1; path[cur].insert(id); if(len[id].find(cur)==len[id].end()) { len[id][cur]=dep; } else { len[id][cur]=min(len[id][cur],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]) { len[chi.fi].clear(); dfs(chi.fi,now,chi.se,chi.fi,1); } pos.insert(0); path[0].insert(now); len[now][0]=0; trav(i,pos) { if(!got[k-i])continue; vpi a; trav(id,path[i]) { a.pb({len[id][i],id}); } sort(all(a)); vpi b; trav(id,path[k-i]) { b.pb({len[id][k-i],id}); } sort(all(b)); if(a[0].se==b[0].se) { if(sz(a)>1) { mn=min(mn,a[1].fi+b[0].fi); } if(sz(b)>1) { mn=min(mn,a[0].fi+b[1].fi); } } else { mn=min(mn,a[0].fi+b[0].fi); } } trav(e,pos) { got[e]=0; path[e].clear(); } } 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...