Submission #379260

# Submission time Handle Problem Language Result Execution time Memory
379260 2021-03-17T17:58:34 Z VEGAnn Svjetlo (COCI20_svjetlo) C++14
110 / 110
410 ms 84480 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 ^ 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][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 9 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12268 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 8 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 56576 KB Output is correct
2 Correct 390 ms 79872 KB Output is correct
3 Correct 387 ms 84480 KB Output is correct
4 Correct 344 ms 61184 KB Output is correct
5 Correct 391 ms 70016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 307 ms 35876 KB Output is correct
2 Correct 394 ms 41344 KB Output is correct
3 Correct 410 ms 49280 KB Output is correct
4 Correct 306 ms 43392 KB Output is correct
5 Correct 298 ms 47616 KB Output is correct
6 Correct 262 ms 44160 KB Output is correct
7 Correct 319 ms 49012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12268 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 8 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 269 ms 56576 KB Output is correct
11 Correct 390 ms 79872 KB Output is correct
12 Correct 387 ms 84480 KB Output is correct
13 Correct 344 ms 61184 KB Output is correct
14 Correct 391 ms 70016 KB Output is correct
15 Correct 307 ms 35876 KB Output is correct
16 Correct 394 ms 41344 KB Output is correct
17 Correct 410 ms 49280 KB Output is correct
18 Correct 306 ms 43392 KB Output is correct
19 Correct 298 ms 47616 KB Output is correct
20 Correct 262 ms 44160 KB Output is correct
21 Correct 319 ms 49012 KB Output is correct
22 Correct 340 ms 44032 KB Output is correct
23 Correct 366 ms 46084 KB Output is correct
24 Correct 330 ms 44416 KB Output is correct
25 Correct 340 ms 43008 KB Output is correct
26 Correct 304 ms 50048 KB Output is correct
27 Correct 308 ms 49920 KB Output is correct
28 Correct 249 ms 44800 KB Output is correct
29 Correct 296 ms 48512 KB Output is correct
30 Correct 277 ms 45812 KB Output is correct