Submission #745611

# Submission time Handle Problem Language Result Execution time Memory
745611 2023-05-20T15:56:01 Z MilosMilutinovic Beads and wires (APIO14_beads) C++14
0 / 100
3 ms 4948 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
const ll inf = 1e16;
ll dp[N][2][2]; // node, par edge, bad path
vector<pair<int, int>> g[N];
int n, w[N];
void setup() {
    for (int i = 1; i <= n; i++)
        for (int x = 0; x < 2; x++)
            for (int y = 0; y < 2; y++)
                dp[i][x][y] = -inf;
}
void solve(int x, int fa, int uw) {
    vector<int> ch;
    for (auto& p : g[x]) {
        int y = p.first;
        if (y == fa) continue;
        w[y] = p.second;
        solve(y, x, w[y]);
        ch.push_back(y);
    }
    if (ch.empty()) {
        dp[x][0][0] = 0;
        return;
    }

    // don't take x
    {
        vector<ll> cur(2, -inf);
        cur[0] = 0;
        for (int y : ch) {
            vector<ll> ndp(2, 0);
            ndp[0] = max(ndp[0], cur[0] + dp[y][0][0]);
            ndp[0] = max(ndp[0], cur[0] + dp[y][1][0]);
            ndp[1] = max(ndp[1], cur[0] + dp[y][0][1]);
            ndp[1] = max(ndp[1], cur[0] + dp[y][1][1]);
            ndp[1] = max(ndp[1], cur[1] + dp[y][0][0]);
            ndp[1] = max(ndp[1], cur[1] + dp[y][1][0]);
            swap(cur, ndp);
        }
        dp[x][0][0] = cur[0];
        dp[x][0][1] = cur[1];
    }

    // take x, from bad path
    {
        vector<ll> cur(3, -inf);
        cur[0] = 0;
        for (int y : ch) {
            vector<ll> ndp(3, -inf);

            // don't take y
            for (int i = 0; i < 3; i++) {
                ndp[i] = max(ndp[i], cur[i] + dp[y][0][0]);
                ndp[i] = max(ndp[i], cur[i] + dp[y][1][0]);
            }

            // take y
            for (int i = 0; i < 2; i++) {
                ndp[i + 1] = max(ndp[i + 1], cur[i] + dp[y][0][0] + w[y]);
                ndp[i + 1] = max(ndp[i + 1], cur[i] + dp[y][0][1] + w[y]);
            }

            swap(cur, ndp);
        }
        dp[x][0][1] = cur[2];
    }

    // take x, no bad path
    {
        vector<ll> cur(2, -inf);
        cur[0] = uw;
        for (int y : ch) {
            vector<ll> ndp(2, -inf);

            // don't take y
            for (int i = 0; i < 2; i++) {
                ndp[i] = max(ndp[i], cur[i] + dp[y][0][0]);
                ndp[i] = max(ndp[i], cur[i] + dp[y][1][0]);
            }

            // take y
            for (int i = 0; i < 1; i++) {
                ndp[i + 1] = max(ndp[i + 1], cur[i] + dp[y][0][0] + w[y]);
                ndp[i + 1] = max(ndp[i + 1], cur[i] + dp[y][0][1] + w[y]);
            }

            swap(cur, ndp);
        }
        dp[x][1][0] = cur[1];
    }

}
int main() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    setup();
    solve(1, 1, -inf / 10);
    printf("%lld\n", max(dp[1][0][0], dp[1][0][1]));
    return 0;
}

/*
5
1 2 10
1 3 40
1 4 15
1 5 20

60

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

140


6
1 3 100
2 3 100
3 4 1
4 5 100
4 6 100
*/

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:105:22: warning: overflow in conversion from 'long long int' to 'int' changes value from '-1000000000000000' to '1530494976' [-Woverflow]
  105 |     solve(1, 1, -inf / 10);
      |                 ~~~~~^~~~
beads.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
beads.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         scanf("%d%d%d", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 3 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 3 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 3 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Incorrect 3 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -