Submission #900492

# Submission time Handle Problem Language Result Execution time Memory
900492 2024-01-08T11:17:53 Z Trisanu_Das Spring cleaning (CEOI20_cleaning) C++17
100 / 100
328 ms 24144 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define pb push_back
const int N = 200050;

int deg[N];
vector<int> E[N];
const int M = 2 * N;
int root, ls[M], rs[M], sum[M], tsz, lzy[M];

void Flip(int &c, int ss, int se, int qs, int qe) {
    if (qs > qe || qs > se || ss > qe) return;
    if (!c) c = ++tsz;
    if (qs <= ss && qe >= se) {
        sum[c] = se - ss + 1 - sum[c];
        lzy[c] ^= 1;
        return;
    }
    int mid = ss + se >> 1;
    Flip(ls[c], ss, mid, qs, qe);
    Flip(rs[c], mid + 1, se, qs, qe);
    sum[c] = sum[ls[c]] + sum[rs[c]];
    if (lzy[c]) sum[c] = se - ss + 1 - sum[c];
}

int head[N], lid[N], rid[N], tid, sz[N], par[N];

void DFS(int u, int p) {
    sz[u] = 1;
    par[u] = p;
    for (int v : E[u]) if (v != p) DFS(v, u), sz[u] += sz[v];
}

void HLD(int u, int p) {
    lid[u] = ++tid;
    if (!head[u]) head[u] = u;
    int hc = 0;
    for (int v : E[u]) if (v != p && sz[hc] < sz[v]) hc = v;
    if (hc != 0) {
        head[hc] = head[u];
        HLD(hc, u);
        for (int v : E[u]) if (v != p && v != hc) HLD(v, u);
    }
    rid[u] = tid;
}

void Flip(int u) {
    while (u > 0) {
        Flip(root, 2, tid, lid[head[u]], lid[u]);
        u = par[head[u]];
    }
}

int main() {
    int n, q;
    cin >> n >> q;

    for (int i = 1, u, v; i < n; i++) {
        cin >> u >> v;
        E[u].pb(v);
        E[v].pb(u);
        deg[u]++;
        deg[v]++;
    }

    DFS(1, 0);
    HLD(1, 0);

    int leaf = 0;

    for (int i = 1; i <= n; i++) if (deg[i] == 1) Flip(i), leaf++;

    for (int i = 1; i <= q; i++) {
        int k;
        cin >> k;

        vector<int> p(k);
        for (int &j : p) {
            cin >> j;
            deg[j]++;
            if (deg[j] != 2) {
                leaf++;
                Flip(j);
            }
        }

        if (leaf % 2 == 1) cout << "-1\n";
        else cout << n - 1 + k + n - 1 - sum[root] << '\n';

        reverse(p.begin(), p.end());

        for (int j : p) {
            deg[j]--;
            if (deg[j] != 1) {
                leaf--;
                Flip(j);
            }
        }
    }

    return 0;
}

Compilation message

cleaning.cpp: In function 'void Flip(int&, int, int, int, int)':
cleaning.cpp:22:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |     int mid = ss + se >> 1;
      |               ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 99 ms 12648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 11864 KB Output is correct
2 Correct 38 ms 11868 KB Output is correct
3 Correct 67 ms 17724 KB Output is correct
4 Correct 82 ms 16592 KB Output is correct
5 Correct 97 ms 18500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 12376 KB Output is correct
2 Correct 45 ms 12376 KB Output is correct
3 Correct 78 ms 23924 KB Output is correct
4 Correct 118 ms 24144 KB Output is correct
5 Correct 68 ms 22444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 13392 KB Output is correct
2 Correct 42 ms 12380 KB Output is correct
3 Correct 26 ms 12204 KB Output is correct
4 Correct 16 ms 12636 KB Output is correct
5 Correct 18 ms 12892 KB Output is correct
6 Correct 51 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 15864 KB Output is correct
2 Correct 202 ms 15696 KB Output is correct
3 Correct 149 ms 13680 KB Output is correct
4 Correct 206 ms 15660 KB Output is correct
5 Correct 202 ms 15748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 328 ms 18772 KB Output is correct
2 Correct 254 ms 21428 KB Output is correct
3 Correct 291 ms 20800 KB Output is correct
4 Correct 268 ms 21808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 99 ms 12648 KB Output is correct
3 Correct 36 ms 11864 KB Output is correct
4 Correct 38 ms 11868 KB Output is correct
5 Correct 67 ms 17724 KB Output is correct
6 Correct 82 ms 16592 KB Output is correct
7 Correct 97 ms 18500 KB Output is correct
8 Correct 45 ms 12376 KB Output is correct
9 Correct 45 ms 12376 KB Output is correct
10 Correct 78 ms 23924 KB Output is correct
11 Correct 118 ms 24144 KB Output is correct
12 Correct 68 ms 22444 KB Output is correct
13 Correct 67 ms 13392 KB Output is correct
14 Correct 42 ms 12380 KB Output is correct
15 Correct 26 ms 12204 KB Output is correct
16 Correct 16 ms 12636 KB Output is correct
17 Correct 18 ms 12892 KB Output is correct
18 Correct 51 ms 12892 KB Output is correct
19 Correct 225 ms 15864 KB Output is correct
20 Correct 202 ms 15696 KB Output is correct
21 Correct 149 ms 13680 KB Output is correct
22 Correct 206 ms 15660 KB Output is correct
23 Correct 202 ms 15748 KB Output is correct
24 Correct 328 ms 18772 KB Output is correct
25 Correct 254 ms 21428 KB Output is correct
26 Correct 291 ms 20800 KB Output is correct
27 Correct 268 ms 21808 KB Output is correct
28 Correct 180 ms 15328 KB Output is correct
29 Correct 196 ms 20648 KB Output is correct
30 Correct 97 ms 18756 KB Output is correct
31 Correct 147 ms 24144 KB Output is correct
32 Correct 208 ms 15676 KB Output is correct
33 Correct 138 ms 18912 KB Output is correct
34 Correct 174 ms 20956 KB Output is correct
35 Correct 185 ms 20828 KB Output is correct