Submission #967413

#TimeUsernameProblemLanguageResultExecution timeMemory
967413steveonalexBeads and wires (APIO14_beads)C++17
100 / 100
127 ms27832 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 2e5 + 69;
const ll INF = 1e18 + 69;

int n;
vector<pair<int, int>> graph[N];
ll dp[N][2];
ll ans = 0;

void dfs(int u, int p){
    for(pair<int, int> v: graph[u]) if (v.first != p){
        dfs(v.first, u);
    }

    ll cu = 0;
    ll ma = -INF;
    for(pair<int, int> v: graph[u]) if (v.first != p){
        dp[u][0] += max(dp[v.first][1] + v.second, dp[v.first][0]);
        cu += max(dp[v.first][1] + v.second, dp[v.first][0]);
        maximize(ma, (v.second + dp[v.first][0]) - max(dp[v.first][1] + v.second, dp[v.first][0]));
    }
    if (ma > -INF){
        cu += ma;
        maximize(dp[u][1], cu);
    }
}

void go(int u, int p){
    maximize(ans, dp[u][0]);
    ll cu = 0;
    ll ma = -INF, secondMa = -INF;
    for(pair<int, int> v: graph[u]){
        cu += max(dp[v.first][1] + v.second, dp[v.first][0]);
        ll val = (v.second + dp[v.first][0]) - max(dp[v.first][1] + v.second, dp[v.first][0]);
        if (ma < val){
            secondMa = ma;
            ma = val;
        }
        else maximize(secondMa, val);
    }
    maximize(ans, cu + ma + secondMa);

    for(pair<int, int> v: graph[u]) if (v.first != p && graph[v.first].size() > 1){
        ll x1 = dp[u][0], x2 = dp[u][1];
        ll y1 = dp[v.first][0], y2 = dp[v.first][1];

        dp[u][0] -= max(dp[v.first][1] + v.second, dp[v.first][0]);
        dp[u][1] -= max(dp[v.first][1] + v.second, dp[v.first][0]);
        ll val = (v.second + dp[v.first][0]) - max(dp[v.first][1] + v.second, dp[v.first][0]);
        if (val == ma) dp[u][1] += secondMa - ma;

        dp[v.first][0] += max(dp[u][1] + v.second, dp[u][0]);
        dp[v.first][1] = 0;
        ll ma = -INF;
        for(pair<int, int> w: graph[v.first]) {
            dp[v.first][1] += max(dp[w.first][1] + w.second, dp[w.first][0]);
            maximize(ma, (w.second + dp[w.first][0]) - max(dp[w.first][1] + w.second, dp[w.first][0]));
        }
        dp[v.first][1] += ma;

        go(v.first, u);

        dp[u][0] = x1, dp[u][1] = x2;
        dp[v.first][0] = y1, dp[v.first][1] = y2;
    }
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;
    for(int i = 1; i<n; ++i){
        int u, v, w; cin >> u >> v >> w;
        graph[u].push_back({v, w});
        graph[v].push_back({u, w});
    }

    if (n <= 2){cout << 0 << "\n"; return 0;}

    for(int i = 1; i<=n; ++i) sort(ALL(graph[i]));

    int root = -1;
    for(int i = 1; i<=n; ++i) if (graph[i].size() > 1) root = i;
    for(int i = 1; i<=n; ++i) {
        dp[i][0] = 0;
        dp[i][1] = -INF;
    }
    dfs(root, root);
    go(root,root);


    cout << ans << "\n";

    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...