Submission #557681

#TimeUsernameProblemLanguageResultExecution timeMemory
557681JomnoiBeads and wires (APIO14_beads)C++17
100 / 100
315 ms33268 KiB
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 5; const int INF = 1e9 + 7; int N; int ans; vector <pair <int, int>> graph[MAX_N]; int dp[MAX_N][2], pdp[MAX_N]; void dfs(int u, int p) { dp[u][1] = -INF; for(auto [v, w] : graph[u]) { if(v == p) { continue; } dfs(v, u); dp[u][0] += max(dp[v][0], dp[v][1] + w); } for(auto [v, w] : graph[u]) { if(v == p) { continue; } dp[u][1] = max(dp[u][1], dp[u][0] - max(dp[v][0], dp[v][1] + w) + dp[v][0] + w); } } void dfs2(int u, int p, int pw) { ans = max(ans, dp[u][0] + pdp[u]); multiset <int> siblings; for(auto [v, w] : graph[u]) { if(v == p) { continue; } siblings.insert(dp[v][0] + w - max(dp[v][0], dp[v][1] + w)); pdp[v] = max(pdp[v], dp[u][0] - max(dp[v][0], dp[v][1] + w) + pdp[u]); if(p != -1) { pdp[v] = max(pdp[v], w + pw + dp[u][0] - max(dp[v][0], dp[v][1] + w) + pdp[p] + dp[p][0] - max(dp[u][0], dp[u][1] + pw)); } } if(siblings.size() > 1) { for(auto [v, w] : graph[u]) { if(v == p) { continue; } siblings.erase(siblings.lower_bound(dp[v][0] + w - max(dp[v][0], dp[v][1] + w))); pdp[v] = max(pdp[v], pdp[u] + dp[u][0] + w - max(dp[v][0], dp[v][1] + w) + *siblings.rbegin()); siblings.insert(dp[v][0] + w - max(dp[v][0], dp[v][1] + w)); } } for(auto [v, w] : graph[u]) { if(v != p) { dfs2(v, u, w); } } } int main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> N; for(int i = 1; i <= N - 1; i++) { int a, b, c; cin >> a >> b >> c; graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); } dfs(1, -1); dfs2(1, -1, -INF); cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...