Submission #546246

# Submission time Handle Problem Language Result Execution time Memory
546246 2022-04-06T19:48:54 Z Ooops_sorry Beads and wires (APIO14_beads) C++14
0 / 100
3 ms 4948 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define ld double
#define ll __int128

mt19937 rnd(51);

const int N = 2e5 + 10, INF = 1e18;
vector<pair<int,int>> g[N];
int res[N][3];

void dfs(int v, int p) {
    bool was = 0;
    vector<vector<int>> dp(3, vector<int>(3, -INF));
    dp[0][0] = 0;
    for (auto [to, c] : g[v]) {
        if (to != p) {
            dfs(to, v);
            was = 1;
            vector<vector<int>> dpp(3, vector<int>(3, -INF));
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                    dpp[j][k] = max({dpp[j][k], dp[j][k] + res[to][2] + c, dp[j][k] + res[to][1]});
                    if (j + 1 < 3) {
                        if (k != 1) dpp[j + 1][k] = max(dpp[j + 1][k], dp[j][k] + res[to][1] + c);
                    }
                    if (k == 0) {
                        if (j == 0) {
                            dpp[j][1] = max(dpp[j][1], dp[j][k] + res[to][0]);
                        }
                        if (j + 1 < 3) {
                            dpp[j + 1][2] = max(dpp[j + 1][2], dp[j][k] + res[to][0] + c);
                        }
                    } else if (k == 2) {
                        if (j == 1) {
                            dpp[j + 1][2] = max(dpp[j + 1][2], dp[j][k] + res[to][0] + c);
                        }
                    }
                }
            }
            swap(dp, dpp);
        }
    }
    if (!was) {
        res[v][0] = -INF;
        res[v][1] = 0;
        res[v][2] = -INF;
    } else {
        res[v][0] = max({dp[2][0], dp[2][1], dp[2][2], dp[0][1]});
        res[v][1] = dp[0][0];
        res[v][2] = max({dp[1][0], dp[1][1]});
    }
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        g[a].pb({b, c});
        g[b].pb({a, c});
    }
    dfs(0, -1);
    cout << max(res[0][0], res[0][1]) << endl;
    return 0;
}

Compilation message

beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:20:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   20 |     for (auto [to, c] : g[v]) {
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Incorrect 3 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -