Submission #1084198

#TimeUsernameProblemLanguageResultExecution timeMemory
1084198f0rizenRace (IOI11_race)C++17
100 / 100
657 ms42500 KiB
#include "race.h" #include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll infll = 1e18; struct Edge { int to, w; }; int k; vector<vector<Edge>> g; vector<bool> used; vector<int> sz; map<ll, int> mp; int ans = inf; void dfs_sz(int v, int p = -1) { sz[v] = 1; for (auto [u, w] : g[v]) { if (u != p && !used[u]) { dfs_sz(u, v); sz[v] += sz[u]; } } } void dfs_ans(int v, int p = -1, int sum = 0, int d = 0) { if (mp.find(k - sum) != mp.end()) { ans = min(ans, mp[k - sum] + d); } for (auto [u, w] : g[v]) { if (u != p && !used[u]) { dfs_ans(u, v, sum + w, d + 1); } } } void dfs_mp(int v, int p = -1, int sum = 0, int d = 0) { if (mp.find(sum) == mp.end()) { mp[sum] = d; } else { mp[sum] = min(mp[sum], d); } for (auto [u, w] : g[v]) { if (u != p && !used[u]) { dfs_mp(u, v, sum + w, d + 1); } } } int centroid(int v, int p, int n) { for (auto [u, w] : g[v]) { if (u != p && !used[u]) { if (sz[u] * 2 > n) { return centroid(u, v, n); } } } return v; } void build(int v) { dfs_sz(v); mp[0] = 0; for (auto [u, w] : g[v]) { if (!used[u]) { dfs_ans(u, v, w, 1); dfs_mp(u, v, w, 1); } } mp.clear(); used[v] = 1; for (auto [u, w] : g[v]) { if (!used[u]) { build(centroid(u, v, sz[u])); } } } int best_path(int N, int K, int H[][2], int L[]) { k = K; g.resize(N); for (int i = 0; i < N - 1; ++i) { int u = H[i][0], v = H[i][1], w = L[i]; g[u].push_back({v, w}); g[v].push_back({u, w}); } used.resize(N); sz.resize(N); dfs_sz(0); build(centroid(0, -1, sz[0])); if (ans == inf) { return -1; } else { 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...