Submission #751994

#TimeUsernameProblemLanguageResultExecution timeMemory
751994vjudge1Beads and wires (APIO14_beads)C++17
0 / 100
3 ms5104 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][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});
    }
    dfs(1, 0);
    cout << dp[1][0] << '\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...