Submission #235829

# Submission time Handle Problem Language Result Execution time Memory
235829 2020-05-30T04:26:33 Z balbit Beads and wires (APIO14_beads) C++14
0 / 100
7 ms 4992 KB
#include <bits/stdc++.h>
using namespace std;


#define ll long long

#define int ll

#define pii pair<int, int>
#define f first
#define s second

#define REP(i,n) for (int i = 0; i<n; ++i)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template<typename T> void _do(T && x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y){cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif // BALBIT

#define SZ(x) (int)(x.size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
const int maxn = 2e5+5;
vector<pii> g[maxn];
ll dp[maxn][2];

void dfs(int v, int p, ll w) {
    vector<ll> mo;
    ll sg = 0;
    for (pii u : g[v]) {
        if (u.f == p) continue;
        dfs(u.f,v,u.s);
        mo.pb(dp[u.f][0] - dp[u.f][1] + u.s);
        sg += dp[u.f][1];
    }
    sort(ALL(mo), greater<ll>());
    dp[v][0] = sg;
    if (SZ(mo) >= 2) {
        dp[v][0] = max(dp[v][0], sg + mo[0] + mo[1]);
    }
    dp[v][1] = dp[v][0];
    if (SZ(mo) >= 1) {
        dp[v][1] = max(dp[v][1], sg + mo[0] + w);
    }
}

signed main(){
    IOS();

    int n; cin>>n;
    if (n <= 2) assert(0);
    for (int i = 0; i<n-1; ++i) {
        int a,b,w; cin>>a>>b>>w;
        --a;--b;g[a].pb({b,w});g[b].pb({a,w});
    }
    dfs(0,-1,0);
    cout<<dp[0][0]<<endl;

}
/*
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Incorrect 7 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Incorrect 7 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Incorrect 7 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Incorrect 7 ms 4992 KB Output isn't correct
7 Halted 0 ms 0 KB -