Submission #198285

#TimeUsernameProblemLanguageResultExecution timeMemory
198285parsa_mobedBeads and wires (APIO14_beads)C++14
100 / 100
354 ms23920 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, inf = 1e9 + 5; int dp[N][2], dpU[N][2], par[N], mx[N], E[N][3], MX1[N], MX2[N]; vector <int> g[N]; inline int adj(int v, int i) {return (v^E[i][0]^E[i][1]);} void dfs(int v) { for (int i : g[v]) if (adj(v, i) != par[v]) { par[adj(v, i)] = v; dfs(adj(v, i)); mx[adj(v, i)] = max(dp[adj(v, i)][1] + E[i][2], dp[adj(v, i)][0]); dp[v][0] += mx[adj(v, i)]; } for (int i : g[v]) if (adj(v, i) != par[v]) { if (dp[adj(v, i)][0] - mx[adj(v, i)] + E[i][2] > MX1[v]) MX2[v] = MX1[v], MX1[v] = dp[adj(v, i)][0] - mx[adj(v, i)] + E[i][2]; else if (dp[adj(v, i)][0] - mx[adj(v, i)] + E[i][2] > MX2[v]) MX2[v] = dp[adj(v, i)][0] - mx[adj(v, i)] + E[i][2]; dp[v][1] = max(dp[v][1], dp[v][0] - mx[adj(v, i)] + dp[adj(v, i)][0] + E[i][2]); } } void dfsUp(int v, int w = 0) { int p = par[v]; if (p != 0) { int x = dpU[p][0] + dp[p][0] - mx[v] + w; dpU[v][0] = x - w, dpU[v][1] = x;; if (MX1[p] != dp[v][0] - mx[v] + w) dpU[v][0] = max(dpU[v][0], MX1[p] + x); else dpU[v][0] = max(dpU[v][0], MX2[p] + x); dpU[v][0] = max(dpU[v][0], dpU[p][1] - dpU[p][0] + x); } for (int i : g[v]) if (adj(v, i) != p) dfsUp(adj(v, i), E[i][2]); } int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> E[i][0] >> E[i][1] >> E[i][2]; g[E[i][0]].push_back(i); g[E[i][1]].push_back(i); } for (int i = 1; i <= n; i++) MX1[i] = MX2[i] = dp[i][1] = dpU[i][1] = -inf; dfs(1), dfsUp(1); int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, dp[i][0] + dpU[i][0]); cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...