Submission #123443

# Submission time Handle Problem Language Result Execution time Memory
123443 2019-07-01T12:46:42 Z popovicirobert Beads and wires (APIO14_beads) C++14
28 / 100
9 ms 5368 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44


/*const int MOD = ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}


inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}*/


using namespace std;

const int INF = 2e9;
const int MAXN = (int) 2e5;

vector < pair <int, int> > g[MAXN + 1];
ll dp[MAXN + 1][2], cst[MAXN + 1];
// dp[nod][0] = nu ai spart nimic
// dp[nod][1] = ai spart un fiu de tata

void dfs(int nod, int par) {
    vector <ll> cur(2, -INF);
    cur[0] = 0;

    for(auto it : g[nod]) {
        if(it.first != par) {
            cst[it.first] = it.second;
            dfs(it.first, nod);

            ll mx = max(dp[it.first][1] + it.second, dp[it.first][0]);
            cur[1] = max(cur[0] + dp[it.first][0] + it.second, cur[1] + mx);
            cur[0] += mx;
        }
    }
    dp[nod][0] = cur[0], dp[nod][1] = cur[1];
}

ll ans;

void solve(int nod, int par) {
    ans = max(ans, dp[nod][0]);

    vector < pair <ll, int> > vals;
    for(auto it : g[nod]) {
        vals.push_back({-(dp[it.first][0] + it.second - max(dp[it.first][0], dp[it.first][1] + it.second)), it.first});
    }
    sort(vals.begin(), vals.end());

    for(auto it : g[nod]) {
        if(it.first != par) {
            ll a = dp[nod][0], b = dp[nod][1], c = dp[it.first][0], d = dp[it.first][1];
            ll mx = max(dp[it.first][0], dp[it.first][1] + it.second);

            dp[nod][0] -= mx;
            if(it.first != vals[0].second) {
                dp[nod][1] -= mx;
            }
            else {
                if(vals.size() > 1) {
                    dp[nod][1] -= dp[it.first][0] + it.second;
                    int id = vals[1].second;
                    dp[nod][1] += dp[id][0] + cst[id] - max(dp[id][0], dp[id][1] + cst[id]);
                }
                else {
                    dp[nod][1] = -INF;
                }
            }

            mx = max(dp[nod][0], dp[nod][1] + it.second);
            dp[it.first][1] = max(dp[it.first][1] + mx, dp[it.first][0] + dp[nod][0] + it.second);
            dp[it.first][0] += mx;

            solve(it.first, nod);

            dp[nod][0] = a, dp[nod][1] = b, dp[it.first][0] = c, dp[it.first][1] = d;
        }
    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    for(i = 1; i < n; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }

    dfs(1, 0);
    solve(1, 0);

    cout << ans;

    //cin.close();
    //cout.close();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 7 ms 5084 KB Output is correct
3 Correct 6 ms 5156 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5084 KB Output is correct
6 Correct 7 ms 4984 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5116 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 7 ms 5084 KB Output is correct
3 Correct 6 ms 5156 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5084 KB Output is correct
6 Correct 7 ms 4984 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5116 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5104 KB Output is correct
13 Correct 6 ms 4984 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 6 ms 4984 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 5072 KB Output is correct
21 Correct 6 ms 4984 KB Output is correct
22 Correct 6 ms 5156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 7 ms 5084 KB Output is correct
3 Correct 6 ms 5156 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5084 KB Output is correct
6 Correct 7 ms 4984 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5116 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5104 KB Output is correct
13 Correct 6 ms 4984 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 6 ms 4984 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 5072 KB Output is correct
21 Correct 6 ms 4984 KB Output is correct
22 Correct 6 ms 5156 KB Output is correct
23 Incorrect 9 ms 5368 KB Output isn't correct
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 7 ms 5084 KB Output is correct
3 Correct 6 ms 5156 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5084 KB Output is correct
6 Correct 7 ms 4984 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4984 KB Output is correct
9 Correct 6 ms 5116 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5104 KB Output is correct
13 Correct 6 ms 4984 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 6 ms 4984 KB Output is correct
16 Correct 6 ms 5112 KB Output is correct
17 Correct 6 ms 5112 KB Output is correct
18 Correct 6 ms 4984 KB Output is correct
19 Correct 6 ms 4984 KB Output is correct
20 Correct 6 ms 5072 KB Output is correct
21 Correct 6 ms 4984 KB Output is correct
22 Correct 6 ms 5156 KB Output is correct
23 Incorrect 9 ms 5368 KB Output isn't correct
24 Halted 0 ms 0 KB -