Submission #41182

#TimeUsernameProblemLanguageResultExecution timeMemory
41182Just_Solve_The_ProblemRace (IOI11_race)C++11
0 / 100
5 ms2864 KiB
#include <race.h> #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair < int, int > #define mk make_pair #define fr first #define sc second #define pb push_back #define ok puts("ok"); const int N = (int)1e5 + 7; int can[N * 10], mnpath[N * 10]; int n, k, book; vector < pii > gr[N]; int sz[N]; bool del[N]; int ans = -1, curmx; void dfs(int v, int pr) { sz[v] = 1; for (auto to : gr[v]) { if (to.fr == pr || del[to.fr]) continue; dfs(to.fr, v); sz[v] += sz[to.fr]; } } int findcentroid(int v) { int now = v; int siz = sz[v]; int pr = -1; bool fl = 1; while (1) { fl = 1; for (auto to : gr[now]) { if (to.fr == pr || del[to.fr]) continue; if (sz[to.fr] * 2 >= siz) { pr = now; now = to.fr; fl = 0; break; } } if (fl) break; } return now; } void dfs1(int v, int pr, int curcost, int curlen, int tp) { if (curcost > k) return ; if (tp == 0) { if (can[k - curcost] == book) { if (curlen + mnpath[k - curcost] < ans || ans == -1) { ans = curlen + mnpath[k - curcost]; } if (curcost == k || ans == -1) { ans = curlen; } } } else { if (can[curcost] < book) { can[curcost] = book; mnpath[curcost] = curlen; } else if (curlen < mnpath[curcost]) { can[curcost] = book; mnpath[curcost] = curlen; } } for (auto to : gr[v]) { if (to.fr == pr || del[to.fr]) continue; dfs1(to.fr, v, curcost + to.sc, curlen + 1, tp); } } void compute(int v) { dfs(v, 0); if (sz[v] <= 1) return ; int centroid = -1; curmx = sz[v] + 3; centroid = findcentroid(v); book++; for (auto to : gr[centroid]) { if (del[to.fr]) continue; dfs1(to.fr, centroid, to.sc, 1, 0); dfs1(to.fr, centroid, to.sc, 1, 1); } // printf("### %d %d\n", centroid, ans); del[centroid] = 1; for (auto to : gr[centroid]) { if (del[to.fr]) continue; compute(to.fr); } } int best_path (int _n, int _k, int _h[][2], int _l[]) { n = _n; k = _k; for (int i = 0; i < n - 1; i++) { int u, v, w; u = _h[i][0] + 1; v = _h[i][1] + 1; w = _l[i]; gr[v].pb(mk(u, w)); gr[u].pb(mk(v, w)); } compute(1); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...