제출 #136121

#제출 시각아이디문제언어결과실행 시간메모리
136121fredbr구슬과 끈 (APIO14_beads)C++17
57 / 100
1062 ms34696 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*3]; vector<FEdge> eg; int dp[2*maxn][2]; inline int invalid(int x) { return v[x].size() == 1; } int solve(int id, int t = 0) { int x = eg[id].b; if (dp[id][t] != -1) return dp[id][t]; int& d = dp[id][t]; d = 0; if (t == 0) { for (Edge& e : v[x]) { if (id == (e.id^1)) continue; if (!invalid(e.u)) { d += max(solve(e.id, 1) + e.w, solve(e.id, 0)); } } } else { int sum = solve(id, 0); for (Edge& e : v[x]) { if (id == (e.id^1)) continue; if (!invalid(e.u)) 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 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}); } int ans = 0; for (int i = 1; i <= n; i++) { if (v[i].size() == 1) { v[n+1].push_back({i, -inf, 2*n-2}); dp[2*n-2][0] = dp[2*n-2][1] = -1; eg.push_back({n+1, i, -inf}); ans = max(ans, solve(2*n-2)); eg.pop_back(); dp[2*n-2][0] = dp[2*n-2][1] = -1; v[n+1].clear(); } } 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...