Submission #546232

# Submission time Handle Problem Language Result Execution time Memory
546232 2022-04-06T17:41:00 Z Ooops_sorry Beads and wires (APIO14_beads) C++14
0 / 100
4 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);

struct edge {
    int a, b, c;
};

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

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

int solve(int n, vector<edge> u) {
    for (int i = 0; i < n; i++) {
        g[i].clear();
        res[i][0] = res[i][1] = 0;
    }
    for (auto to : u) {
        g[to.a].pb({to.b, to.c});
        g[to.b].pb({to.a, to.c});
    }
    dfs(0, -1);
    return res[0][0];
}

int zhfs(int v, int p) {
    int cnt = 0;
    for (auto to : g[v]) {
        if (to.first != p) {
            int val = zhfs(to.first, v);
            if (val == 1) {
                if (to.second == 0) {
                    bad = 1;
                }
            } else {
                cnt += to.second;
            }
        }
    }
    if (cnt > 3) bad = 1;
    return cnt % 2;
}

int solvee(int n, vector<edge> u) {
    n--;
    int ans = 0;
    for (int i = 0; i < (1 << n); i++) {
        int res = 0;
        for (int j = 0; j < n + 1; j++) g[j].clear();
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                res += u[j].c;
                g[u[j].a].pb({u[j].b, 1});
                g[u[j].b].pb({u[j].a, 1});
            } else {
                g[u[j].a].pb({u[j].b, 0});
                g[u[j].b].pb({u[j].a, 0});
            }
        }
        bad = 0;
        int val = zhfs(0, -1);
        if (val == 1) bad = 1;
        if (!bad) {
            ans = max(ans, res);
        }
    }
    return ans;
}

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

Compilation message

beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:24:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   24 |     for (auto [to, c] : g[v]) {
      |               ^
# 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 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Incorrect 4 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 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Incorrect 4 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 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Incorrect 4 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 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Incorrect 4 ms 4948 KB Output isn't correct
7 Halted 0 ms 0 KB -