Submission #135292

#TimeUsernameProblemLanguageResultExecution timeMemory
135292fredbrBeads and wires (APIO14_beads)C++17
57 / 100
483 ms49944 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int const maxn = 202020; 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.id == (id^1)) 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.id == (id^1)) continue; // cout << e.u << "\n"; 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"; }

Compilation message (stderr)

beads.cpp: In function 'int solve(int, int)':
beads.cpp:29:9: warning: unused variable 'p' [-Wunused-variable]
     int p = eg[id].a;
         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...