Submission #135038

#TimeUsernameProblemLanguageResultExecution timeMemory
135038fredbrBeads and wires (APIO14_beads)C++17
0 / 100
2 ms760 KiB
#include <bits/stdc++.h> using namespace std; int const maxn = 10101; struct Edge { int u, w; }; vector<Edge> v[maxn]; int h[maxn]; int tam(int x, int p = -1) { h[x] = 1; for (Edge e : v[x]) { if (e.u == p) continue; h[x] = max(h[x], tam(e.u, x) + 1); } return h[x]; } int dp[maxn][2]; int solve(int x, int p = -1, int t = 0) { if (dp[x][t] >= 0) return dp[x][t]; int& d = dp[x][t]; d = 0; if (t == 0) { for (Edge e : v[x]) { if (e.u == p) continue; if (h[e.u] > 1) { d += max(solve(e.u, x, 1) + e.w, solve(e.u, x, 0)); } } } else { int sum = 0; for (Edge e : v[x]) { if (e.u == p) continue; if (h[e.u] > 1) sum += solve(e.u, x, 1) + e.w; } for (Edge e : v[x]) { if (e.u == p) continue; if (h[e.u] > 1) d = max(d, sum - solve(e.u, x, 1) + solve(e.u, x, 0)); else d = max(d, sum + e.w); } } return d; } int test(int x) { memset(dp, -1, sizeof(dp)); tam(x); return solve(x); } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n-1; i++) { int a, b, w; cin >> a >> b >> w; v[a].push_back({b, w}); v[b].push_back({a, w}); } int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, test(i)); cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...