Submission #379259

# Submission time Handle Problem Language Result Execution time Memory
379259 2021-03-17T17:54:16 Z VEGAnn Svjetlo (COCI20_svjetlo) C++14
50 / 110
409 ms 84096 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;
//    f[v][tp[v]][0] = 0;

    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;

        /// do smth

//        if (v == 0){
//            cerr << "OK\n";
//        }

        {
            /// 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][0][0]);
//                amin(nf[cl][1], f[v][cl][1] + f[u][1][0] + 2);
                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][1] + min(f[u][0][1], f[u][0][0]) + 1);
//                amin(nf[cl][1], f[v][cl][0] + min(f[u][0][1], f[u][0][0]) + 1);

                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][2], f[v][cl][0] + min(f[u][1][2], min(f[u][1][1], f[u][1][0])));
//                amin(nf[cl ^ 1][2], f[v][cl][0] + min(f[u][0][2], min(f[u][0][1], f[u][0][0])) + 2);
                amin(nf[cl][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][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][2], f[v][cl][1] + min(f[u][0][1], f[u][0][0]) + 1);
//                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 ^ 1][2], f[v][cl][2] + f[u][0][0]);
//                amin(nf[cl][2], f[v][cl][2] + f[u][1][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 8 ms 12140 KB Output is correct
2 Correct 9 ms 12160 KB Output is correct
3 Correct 10 ms 12268 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 268 ms 57472 KB Output is correct
2 Correct 390 ms 80512 KB Output is correct
3 Correct 392 ms 84096 KB Output is correct
4 Correct 353 ms 63360 KB Output is correct
5 Correct 407 ms 72064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 337 ms 41168 KB Output is correct
2 Incorrect 409 ms 48384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 9 ms 12160 KB Output is correct
3 Correct 10 ms 12268 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 8 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 268 ms 57472 KB Output is correct
11 Correct 390 ms 80512 KB Output is correct
12 Correct 392 ms 84096 KB Output is correct
13 Correct 353 ms 63360 KB Output is correct
14 Correct 407 ms 72064 KB Output is correct
15 Correct 337 ms 41168 KB Output is correct
16 Incorrect 409 ms 48384 KB Output isn't correct
17 Halted 0 ms 0 KB -