Submission #104113

#TimeUsernameProblemLanguageResultExecution timeMemory
104113rkocharyanRace (IOI11_race)C++14
43 / 100
3039 ms45944 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 7; const int K = 1e6 + 7; const int LG = 20; const int inf = 1e9 + 7; int n, k; int sz[N]; bool used[N]; vector <pair <int, int> > g[N]; vector <int> kek(K, inf); vector <int> go; int ans = inf; void calc(int v, int pr) { sz[v] = 1; for (auto to : g[v]) { if (to.first != pr && !used[to.first]) { calc(to.first, v); sz[v] += sz[to.first]; } } } int centroid(int v, int pr, int need) { for (auto to : g[v]) { if (to.first != pr && !used[to.first] && sz[to.first] > need / 2) { return centroid(to.first, v, need); } } return v; } void dfs(int v, int pr, int h, int cur) { if (cur > k) { return; } go.push_back(cur); kek[cur] = min(kek[cur], h); for (auto to : g[v]) { if (to.first != pr && !used[to.first]) { dfs(to.first, v, h + 1, cur + to.second); } } } void build(int v, int lg) { calc(v, -1); v = centroid(v, -1, sz[v]); vector <int> dp(k + 1, inf); dp[0] = 0; kek[0] = 0; for (auto to : g[v]) { if (!used[to.first]) { dfs(to.first, v, 1, to.second); go.push_back(0); for (int x : go) { ans = min(ans, kek[x] + dp[k - x]); } for (int x : go) { dp[x] = min(dp[x], kek[x]); kek[x] = inf; } go = {}; } } used[v] = true; for (auto to : g[v]) { if (!used[to.first]) { build(to.first, lg + 1); } } } 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, l; u = H[i][0]; v = H[i][1]; l = L[i]; g[u].push_back({v, l}); g[v].push_back({u, l}); } build(0, 0); return (ans == inf ? -1 : 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...