Submission #379261

# Submission time Handle Problem Language Result Execution time Memory
379261 2021-03-17T18:01:43 Z VEGAnn Svjetlo (COCI20_svjetlo) C++14
110 / 110
433 ms 84736 KB
#include <bits/stdc++.h>
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
const int N = 500100;
const int oo = 1e9; /// (!!!)
vector<int> g[N];
string s;
int n, sub[N], f[N][2][3], root;
int nf[2][3];
bool tp[N];


void amin(int &x, int y){
    x = min(x, y);
}

void dfs(int v, int p){
    sub[v] = bool(s[v] == '0');

    for (int u : g[v]){
        if (u == p) continue;

        dfs(u, v);

        sub[v] += sub[u];
    }

    if (sub[v] == 0) return;

    if (sub[v] == 1 && !tp[v]){
        f[v][tp[v] ^ 1][0] = 1;

        return;
    }

    f[v][tp[v] ^ 1][0] = 1;

    for (int u : g[v]){
        if (u == p) continue;

        if (sub[u] == 0) continue;

        for (int cl = 0; cl < 2; cl++)
        for (int ti = 0; ti < 3; ti++)
            nf[cl][ti] = oo;

        {
            /// ti == 0

            for (int cl = 0; cl < 2; cl++){
                amin(nf[cl ^ 1][0], f[v][cl][0] + f[u][1][0] + 1);
                amin(nf[cl][0], f[v][cl][0] + f[u][0][0] + 3);
            }
        }

        {
            /// ti == 1

            for (int cl = 0; cl < 2; cl++){
                amin(nf[cl ^ 1][1], f[v][cl][1] + f[u][1][0] + 1);
                amin(nf[cl][1], f[v][cl][1] + f[u][0][0] + 3);

                amin(nf[cl][1], f[v][cl][0] + min(f[u][1][1], f[u][1][0]));
                amin(nf[cl ^ 1][1], f[v][cl][0] + min(f[u][0][1], f[u][0][0]) + 2);
            }
        }

        {
            /// ti == 2

            for (int cl = 0; cl < 2; cl++){
                amin(nf[cl ^ 1][2], f[v][cl][0] + min(f[u][1][2], min(f[u][1][1], f[u][1][0])) + 3);
                amin(nf[cl][2], f[v][cl][0] + min(f[u][0][2], min(f[u][0][1], f[u][0][0])) + 1);

                amin(nf[cl][2], f[v][cl][1] + min(f[u][1][1], f[u][1][0]));
                amin(nf[cl ^ 1][2], f[v][cl][1] + min(f[u][0][1], f[u][0][0]) + 2);

                amin(nf[cl ^ 1][2], f[v][cl][2] + f[u][1][0] + 1);
                amin(nf[cl][2], f[v][cl][2] + f[u][0][0] + 3);
            }
        }

        for (int cl = 0; cl < 2; cl++)
        for (int ti = 0; ti < 3; ti++)
            f[v][cl][ti] = nf[cl][ti];
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n >> s;

    for (int i = 0; i < n; i++){
        tp[i] = (s[i] - '0');
    }

    for (int i = 1; i < n; i++){
        int x, y; cin >> x >> y; x--; y--;

        g[x].PB(y);
        g[y].PB(x);
    }

    for (int i = 0; i < n; i++)
    for (int j = 0; j < 2; j++)
    for (int z = 0; z < 3; z++)
        f[i][j][z] = oo;
    root = 0;

    while (root < n && tp[root]) root++;

    if (root == n){
        cout << 0;
        return 0;
    }

    dfs(root, -1);

    int ans = oo;

    for (int ti = 0; ti < 3; ti++)
        amin(ans, f[root][1][ti]);

    cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12160 KB Output is correct
6 Correct 10 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 10 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 269 ms 56832 KB Output is correct
2 Correct 421 ms 80256 KB Output is correct
3 Correct 389 ms 84736 KB Output is correct
4 Correct 345 ms 61312 KB Output is correct
5 Correct 413 ms 70144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 331 ms 35968 KB Output is correct
2 Correct 414 ms 41600 KB Output is correct
3 Correct 433 ms 42368 KB Output is correct
4 Correct 317 ms 37632 KB Output is correct
5 Correct 300 ms 41472 KB Output is correct
6 Correct 274 ms 38528 KB Output is correct
7 Correct 331 ms 42340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 8 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12160 KB Output is correct
6 Correct 10 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 10 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 269 ms 56832 KB Output is correct
11 Correct 421 ms 80256 KB Output is correct
12 Correct 389 ms 84736 KB Output is correct
13 Correct 345 ms 61312 KB Output is correct
14 Correct 413 ms 70144 KB Output is correct
15 Correct 331 ms 35968 KB Output is correct
16 Correct 414 ms 41600 KB Output is correct
17 Correct 433 ms 42368 KB Output is correct
18 Correct 317 ms 37632 KB Output is correct
19 Correct 300 ms 41472 KB Output is correct
20 Correct 274 ms 38528 KB Output is correct
21 Correct 331 ms 42340 KB Output is correct
22 Correct 353 ms 38144 KB Output is correct
23 Correct 357 ms 39780 KB Output is correct
24 Correct 329 ms 38528 KB Output is correct
25 Correct 347 ms 37248 KB Output is correct
26 Correct 307 ms 43520 KB Output is correct
27 Correct 322 ms 43392 KB Output is correct
28 Correct 251 ms 39168 KB Output is correct
29 Correct 299 ms 41984 KB Output is correct
30 Correct 292 ms 39796 KB Output is correct