Submission #31635

#TimeUsernameProblemLanguageResultExecution timeMemory
31635cheater2kBeads and wires (APIO14_beads)C++14
0 / 100
10 ms9728 KiB
#include <bits/stdc++.h> using namespace std; const int N = 200010; const int inf = 2e9 + 12345; int n; int f[N][2]; vector < pair<int,int> > G[N], ch[N]; int ans; void dfs(int u, int p) { for (auto edge: G[u]) if (edge.second != p) { dfs(edge.second, u); ch[u].push_back(edge); } f[u][1] = -inf; f[u][0] = 0; for (auto edge: ch[u]) { f[u][0] += max(f[edge.second][0], f[edge.second][1] + edge.first); } for (auto edge: ch[u]) { int mx = f[u][0]; mx -= max(f[edge.second][0], f[edge.second][1] + edge.first); mx += (f[edge.second][0] + edge.first); f[u][1] = max(f[u][1], mx); } } void re_dfs(int u, int p) { ans = max(ans, f[u][0]); // prepare vector <int> L(n + 2, -inf); vector <int> R(n + 2, -inf); for (int i = 0; i < (int)ch[u].size(); ++i) { pair<int,int> edge = ch[u][i]; L[i + 1] = max(L[i], f[u][0] - max(f[edge.second][0], f[edge.second][1] + edge.first) + (f[edge.second][0] + edge.first)); } for (int i = (int)ch[u].size() - 1; i >= 0; --i) { pair<int,int> edge = ch[u][i]; R[i] = max(R[i + 1], f[u][0] - max(f[edge.second][0], f[edge.second][1] + edge.first) + (f[edge.second][0] + edge.first)); } // solve for (int i = 0; i < (int)ch[u].size(); ++i) { int v = ch[u][i].second, w = ch[u][i].first; int cost = max(f[v][0], f[v][1] + w); int fu0 = f[u][0] - cost; int fu1 = max(L[i], R[i + 1]) - cost; int fv0 = f[v][0] + max(fu0, fu1 + w); int fv1 = max(f[v][1] + max(fu0, fu1 + w), fv0 - max(fu0, fu1 + w) + (fu0 + w)); f[v][0] = fv0; f[v][1] = fv1; re_dfs(v, u); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; G[u].push_back(make_pair(w,v)); G[v].push_back(make_pair(w,u)); } dfs(1,1); re_dfs(1,1); cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...