Submission #537224

#TimeUsernameProblemLanguageResultExecution timeMemory
537224Leonardo_PaesBeads and wires (APIO14_beads)C++17
0 / 100
1 ms468 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> pii; #define f first #define s second const int maxn = 1e4+10, inf = 0x3f3f3f3f; vector<pii> grafo[maxn]; int dp[maxn][2]; // edg + dp[id][0] - dp[id][1]; void solve(int u, int p = 0, int e = -1){ int sum = 0, sons = 0; int mx[2] = {-inf, -inf}; for(pii vv : grafo[u]){ int v = vv.f, w = vv.s; if(v == p) continue; sons++; solve(v, u, w); int aux = w + dp[v][0] - dp[v][1]; mx[1] = max(mx[1], aux); if(mx[0] < mx[1]) swap(mx[0], mx[1]); sum += dp[v][1]; } if(sons >= 2) dp[u][0] = sum + mx[0] + mx[1]; else dp[u][0] = max(dp[grafo[u][0].f][0], dp[grafo[u][0].f][1]); if(e != -1 and mx[0] != -inf) dp[u][1] = e + sum + mx[0]; } int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int n; cin >> n; for(int i=1; i<n; i++){ int u, v, w; cin >> u >> v >> w; grafo[u].push_back({v, w}); grafo[v].push_back({u, w}); } int ans = 0; for(int i=1; i<=n; i++){ solve(i); ans = max(ans, dp[i][0]); //for(int j=1; j<=n; j++) cout << dp[j][0] << " " << dp[j][1] << "\n"; for(int j=1; j<=n; j++) dp[j][0] = dp[j][1] = 0; } cout << ans << "\n"; return 0; /* 10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34 */ }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...