Submission #198258

#TimeUsernameProblemLanguageResultExecution timeMemory
198258parsa_mobedBeads and wires (APIO14_beads)C++14
57 / 100
1066 ms22736 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O2") #pragma GCC optimize ("unroll-loops") using namespace std; #define pii pair <int , int> #define F first #define S second const int N = 2e5 + 10, inf = 1e9 + 5; int dp[N][2], dpU[N][2], par[N], mx[N], E[N][3]; vector <int> g[N]; inline int adj(int v, int i) {return (v^E[i][0]^E[i][1]);} void dfs(int v, int p = 0) { par[v] = p; for (int i : g[v]) if (adj(v, i) != p) { dfs(adj(v, i), v); 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) != p) 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) dpU[v][0] = 0; if (p != 0) { dpU[v][0] = dpU[p][0] + dp[p][0] - mx[v]; for (int i : g[p]) if (adj(p, i) != v && adj(p, i) != par[p]) dpU[v][0] = max(dpU[v][0], dpU[p][0] + dp[p][0] - mx[adj(p, i)] + dp[adj(p, i)][0] + E[i][2] - mx[v] + w); dpU[v][0] = max(dpU[v][0], dpU[p][1] + dp[p][0] - mx[v] + w); dpU[v][1] = dpU[p][0] + dp[p][0] - mx[v] + w; } 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++) 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]); printf("%d", ans); 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...