Submission #889641

#TimeUsernameProblemLanguageResultExecution timeMemory
889641asdfgraceRace (IOI11_race)C++17
21 / 100
464 ms115028 KiB
#include <bits/stdc++.h> #include "race.h" using namespace std; #pragma GCC optimize("O3") /* start at the centroid consider all paths whose lca is the centroid of the tree try solving each of these in o(n)? can we knapsack? consider root & sum of weights on paths to each node do a dfs then for each node search for a matching path */ const int MX = (int) 1e5 + 10; multiset<int> at[1000010]; int sz[MX], val[MX], dep[MX]; vector<pair<int, int>> edges[MX]; bool visited[MX]; int best_path(int N, int K, int H[][2], int L[]) { int ans = 1e9; for (int i = 0; i < N - 1; i++) { edges[H[i][0]].push_back({H[i][1], L[i]}); edges[H[i][1]].push_back({H[i][0], L[i]}); } at[0].insert(0); for (int i = 0; i < N; i++) { sz[i] = 1; val[i] = 0; dep[i] = 0; visited[i] = false; } function<void(int, int)> dfs_sz = [&] (int node, int par) { sz[node] = 1; for (auto [next, wt] : edges[node]) { if (next == par || visited[next]) continue; dfs_sz(next, node); sz[node] += sz[next]; } }; function<int(int, int, int)> centroid = [&] (int node, int par, int s) { for (auto [next, wt] : edges[node]) { if (next != par && !visited[next] && sz[next] > s / 2) { return centroid(next, node, s); } } return node; }; function<void(int, int)> dfs_val = [&] (int node, int par) { for (auto [next, wt] : edges[node]) { if (next != par && !visited[next]) { dep[next] = dep[node] + 1; val[next] = min(val[node] + wt, 1000005); dfs_val(next, node); } } }; function<void(int, int, int)> dfs_add = [&] (int node, int par, int x) { if (x == 1) { at[val[node]].insert(dep[node]); } else if (x == -1) { at[val[node]].erase(at[val[node]].find(dep[node])); } else if (x == 2) { if (K - val[node] >= 0 && at[K - val[node]].size()) { ans = min(ans, dep[node] + *at[K - val[node]].begin()); } } for (auto [next, wt] : edges[node]) { if (next != par && !visited[next]) { dfs_add(next, node, x); } } }; function<void(int, int)> dfs = [&] (int node, int par) { dfs_sz(node, par); int cent = centroid(node, par, sz[node]); visited[cent] = true; dep[cent] = val[cent] = 0; dfs_val(cent, -1); for (auto [next, wt] : edges[cent]) { if (!visited[next]) { dfs_add(next, cent, 1); } } for (auto [next, wt] : edges[cent]) { if (!visited[next]) { dfs_add(next, cent, -1); dfs_add(next, cent, 2); dfs_add(next, cent, 1); } } for (auto [next, wt] : edges[cent]) { if (!visited[next]) { dfs_add(next, cent, -1); } } for (auto [next, wt] : edges[cent]) { if (!visited[next]) { dfs(next, cent); } } }; dfs(0, -1); return (ans == 1e9 ? -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...