Submission #135293

#TimeUsernameProblemLanguageResultExecution timeMemory
135293fredbrBeads and wires (APIO14_beads)C++17
57 / 100
472 ms69788 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int const maxn = 303030; int const inf = 0x3f3f3f3f; struct Edge { int u, w, id; }; struct FEdge { int a, b, w; }; vector<Edge> v[maxn]; vector<FEdge> eg; int dp[4*maxn][2]; inline int invalid(int x, int p) { return v[x].size() == 1; } int solve(int id, int t = 0) { int x = eg[id].b; int p = eg[id].a; if (dp[id][t] >= 0) return dp[id][t]; int d = 0; if (t == 0) { for (Edge const& e : v[x]) { if (e.u == p) continue; if (!invalid(e.u, x)) { d += max(solve(e.id, 1) + e.w, solve(e.id, 0)); } } } else { int sum = solve(id, 0); for (Edge const& e : v[x]) { if (e.u == p) continue; if (!invalid(e.u, x)) d = max(d, sum - max(solve(e.id, 1) + e.w, solve(e.id, 0)) + e.w + solve(e.id, 0)); else d = max(d, sum + e.w); } } return dp[id][t] = d; } int main() { memset(dp, -1, sizeof(dp)); 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, i*2}); v[b].push_back({a, w, i*2+1}); eg.push_back({a, b, w}); eg.push_back({b, a, w}); } for (int i = 1; i <= n; i++) { v[i+n].push_back({i, -inf, 2*n - 3 + i}); eg.push_back({i+n, i, -inf}); } int ans = 0; for (int i = 2*n-2; i < 3*n-2; i++) { ans = max(ans, solve(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...