Submission #751998

#TimeUsernameProblemLanguageResultExecution timeMemory
751998boyliguanhanBeads and wires (APIO14_beads)C++17
0 / 100
4 ms4968 KiB
#include<bits/stdc++.h> using namespace std; long long dp[200100][2]; vector<pair<int, long long>> adj[200100]; void dfs(int n, int p) { dp[n][0] = 0; dp[n][1] = -1e18; long long m1=-1e18, m2=-1e18; for(auto [i, j]: adj[n]) { if(i!=p) { dfs(i, n); long long X = max(dp[i][1]+j, dp[i][0]); dp[n][0]+=X; long long x = dp[i][0] + j - X; if(m1 <= x)m2 = m1, m1 = x; else if(m2<x) m2 = x; } } dp[n][1]=dp[n][0]+m1; dp[n][0]+=max(0LL,m1+m2); } int main() { cin.sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for(int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; adj[a].push_back({b, c}); adj[b].push_back({a, c}); } long long m = -1e18; for(int i = 1; i <= n; i++) { dfs(i, 0); m = max(m, dp[i][0]); } cout << m << '\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...