Submission #31083

#TimeUsernameProblemLanguageResultExecution timeMemory
31083WhipppedCreamRace (IOI11_race)C++14
100 / 100
1327 ms38380 KiB
#include <bits/stdc++.h> #include "race.h" #define ii pair<int, int> #define X first #define Y second #define pb push_back #define vi vector<int> #define vii vector< pair<int, int> > #define iii pair< ii, int > typedef long long ll; using namespace std; vector< ii > adj[200005]; int use[200005]; int deg[200005]; int maxnow[200005]; int mic[200005]; void memsetNode(int u, int par) { mic[u] = -1; maxnow[u] = 0; //printf("set 0 at %d\n", u); for(int i = 0; i< (int) adj[u].size(); i++) { int v = adj[u][i].X; if(use[v]) continue; if(v == par) continue; memsetNode(v, u); } } int cnt(int u, int par) { if(mic[u] != -1) return mic[u]; int sum = 1; for(int i = 0; i< (int) adj[u].size(); i++) { int v = adj[u][i].X; if(use[v]) continue; if(v == par) continue; sum += cnt(v, u); } //printf("computed %d %d\n", u, par); return mic[u] = sum; } void update(int u, int par, int book) { //printf("update %d %d %d\n", u, par, book); maxnow[u] = max(maxnow[u], book); int sm = 1; for(int i = 0; i< (int) adj[u].size(); i++) { ii k = adj[u][i]; if(k.X == par) continue; if(use[k.X]) continue; maxnow[k.X] = max(maxnow[k.X], 1+book); maxnow[u] = max(maxnow[u], cnt(k.X, u)); //printf("added (%d, %d)", k.X, u); sm += cnt(k.X, u); } sm += book; //printf("sm is %d for %d\n", sm, u); for(int i = 0; i< (int) adj[u].size(); i++) { ii k = adj[u][i]; if(k.X == par) continue; if(use[k.X]) continue; update(k.X, u, sm - cnt(k.X, u)); } } int findBest(int u, int par) { int best = -1; //printf("%d: %d\n", u, maxnow[u]); for(int i = 0; i< (int) adj[u].size(); i++) { int v = adj[u][i].X; if(use[v]) continue; if(v == par) continue; int here = findBest(v, u); if(best == -1 || maxnow[here]< maxnow[best]) best = here; } int res; if(best == -1) res = u; else if(maxnow[best]< maxnow[u]) res = best; else res = u; //printf("of size %d\n", maxnow[res]); return res; } int memsetcnt = 0; void findNode(int u, int par) { //printf("its me\n"); int st = u; //printf("--------memsetting %d %d\n", u, par); memsetcnt++; //if(memsetcnt % 10000 == 0) printf("%d\n", memsetcnt/1000); memsetNode(u, par); update(st, par, 0); //printf("kuy\n"); } int bestPath[1000005]; vii joe; void diep(int u, int par, int dist, int len) { if(dist > 1000000) return; joe.pb(ii(dist, len)); //printf("%d %d %d %d\n", u, par, dist, len); for(int i = 0; i< (int) adj[u].size(); i++) { int v = adj[u][i].X; int w = adj[u][i].Y; if(v == par) continue; if(use[v]) continue; diep(v, u, dist+w, len+1); } } int k; int n; int solve(int u, int par) { //memset maxnow 0 //memset mic //printf("hello\n"); findNode(u, par); int root = findBest(u, par); //printf("--------------root is %d\n", root); vector<int> Set; int best = 1e9; bestPath[0] = 0; for(int i = 0; i< (int) adj[root].size(); i++) { joe.clear(); int v = adj[root][i].X; if(v == par) continue; if(use[v]) continue; diep(v, root, adj[root][i].Y, 1); for(int j = 0; j< (int) joe.size(); j++) { Set.pb(joe[j].X); if(k-joe[j].X >= 0 && bestPath[k-joe[j].X] != -1) { best = min(best, bestPath[k-joe[j].X] + joe[j].Y); } } for(int j = 0; j< (int) joe.size(); j++) { if(bestPath[joe[j].X] == -1 || bestPath[joe[j].X] > joe[j].Y) bestPath[joe[j].X] = joe[j].Y; //lprintf("%d\n", joe[j].X); } } use[root] = 1; for(int i = 0; i< (int) Set.size(); i++) { bestPath[Set[i]] = -1; } for(int i = 0; i< (int) adj[root].size(); i++) { int v = adj[root][i].X; if(v == par) continue; if(use[v]) continue; best = min(best, solve(v, root)); } return best; } int best_path(int N, int K, int H[][2], int L[]) { memset(bestPath, -1, sizeof bestPath); memset(mic, -1, sizeof mic); //freopen("grader.in.16", "r", stdin); n = N; k = K; for(int i = 0; i< n-1; i++) { int a, b, w; a = H[i][0], b = H[i][1], w = L[i]; adj[a].pb(ii(b, w)); adj[b].pb(ii(a, w)); deg[a]++; deg[b]++; } int res = solve(0, -1); if(res == 1e9) return -1; return res; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...