Submission #784444

# Submission time Handle Problem Language Result Execution time Memory
784444 2023-07-16T06:37:29 Z t6twotwo Spring cleaning (CEOI20_cleaning) C++17
100 / 100
191 ms 26060 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct node {
    int z, o;
    node() {
        z = o = 0;
    }
    node(int v) {
        if (v == 0) {
            z = 1;
            o = 0;
        } else {
            o = 1;
            z = 0;
        }
    }
    node(int a, int b) {
        z = a;
        o = b;
    }
};
node operator+(const node &a, const node &b) {
    return {a.z + b.z, a.o + b.o};
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, Q;
    cin >> N >> Q;
    vector<int> deg(N);
    vector<vector<int>> adj(N);
    for (int i = 0; i < N - 1; i++) {
        int x, y;
        cin >> x >> y;
        deg[--x]++;
        deg[--y]++;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    vector<int> v(N), par(N, -1), dep(N), sz(N);
    int cnt = 0, leaves = 0;
    auto init = [&](auto init, int x) -> void {
        if (deg[x] == 1) {
            v[x] = 1;
            leaves++;
        }
        if (par[x] != -1) {
            adj[x].erase(find(adj[x].begin(), adj[x].end(), par[x]));
        }
        sz[x] = 1;
        for (int &y : adj[x]) {
            par[y] = x;
            dep[y] = dep[x] + 1;
            init(init, y);
            sz[x] += sz[y];
            v[x] ^= v[y];
            if (sz[y] > sz[adj[x][0]]) {
                swap(y, adj[x][0]);
            }
        }
        cnt += v[x];
    };
    init(init, 0);
    int K = 2 << __lg(N - 1);
    vector<int> top(N), in(N), lazy(2 * K);
    vector<node> st(2 * K);
    int timer = 0;
    auto dfs = [&](auto dfs, int x) -> void {
        in[x] = timer++;
        st[in[x] + K] = v[x];
        for (int y : adj[x]) {
            top[y] = y == adj[x][0] ? top[x] : y;
            dfs(dfs, y);
        }
    };
    dfs(dfs, 0);
    auto apply = [&](int p, int z) {
        if (z == 0) {
            return;
        }
        swap(st[p].z, st[p].o);
        lazy[p] ^= 1;
    };
    auto pull = [&](int p) {
        st[p] = st[p * 2] + st[p * 2 + 1];
    };
    auto push = [&](int p) {
        apply(p * 2, lazy[p]);
        apply(p * 2 + 1, lazy[p]);
        lazy[p] = 0;
    };
    auto update = [&](auto f, int p, int l, int r, int L, int R) -> void {
        if (R <= l || r <= L) {
            return;
        }
        if (L <= l && r <= R) {
            apply(p, 1);
            return;
        }
        int m = (l + r) / 2;
        push(p);
        f(f, p * 2, l, m, L, R);
        f(f, p * 2 + 1, m, r, L, R);
        pull(p);
    };
    auto query = [&](auto f, int p, int l, int r, int L, int R) -> node {
        if (R <= l || r <= L) {
            return {};
        }
        if (L <= l && r <= R) {
            return st[p];
        }
        int m = (l + r) / 2;
        push(p);
        return f(f, p * 2, l, m, L, R) + f(f, p * 2 + 1, m, r, L, R);
    };
    for (int i = K - 1; i; i--) {
        pull(i);
    }
    while (Q--) {
        int D;
        cin >> D;
        vector<int> a(D);
        for (int &p : a) {
            cin >> p;
            p--;
            if (deg[p]++ != 1) {
                leaves++;
                int x = p;
                while (x != -1) {
                    update(update, 1, 0, K, in[top[x]], in[x] + 1);
                    x = par[top[x]];
                }
            }
        }
        if (leaves % 2 == 1) {
            cout << -1 << "\n";
        } else {
            cout << 2 * (N - 1) - st[1].o + D << "\n";
        }
        for (int p : a) {
            if (--deg[p] != 1) {
                leaves--;
                int x = p;
                while (x != -1) {
                    update(update, 1, 0, K, in[top[x]], in[x] + 1);
                    x = par[top[x]];
                }
            }
        }
    }
    return 6/22;
}

Compilation message

cleaning.cpp: In function 'int main()':
cleaning.cpp:107:10: warning: variable 'query' set but not used [-Wunused-but-set-variable]
  107 |     auto query = [&](auto f, int p, int l, int r, int L, int R) -> node {
      |          ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 320 KB Output is correct
2 Correct 91 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 1380 KB Output is correct
2 Correct 45 ms 1376 KB Output is correct
3 Correct 29 ms 12612 KB Output is correct
4 Correct 58 ms 10412 KB Output is correct
5 Correct 57 ms 12940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 2372 KB Output is correct
2 Correct 37 ms 2260 KB Output is correct
3 Correct 39 ms 26060 KB Output is correct
4 Correct 105 ms 25336 KB Output is correct
5 Correct 33 ms 24484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 4704 KB Output is correct
2 Correct 33 ms 3168 KB Output is correct
3 Correct 7 ms 2988 KB Output is correct
4 Correct 8 ms 3924 KB Output is correct
5 Correct 9 ms 4220 KB Output is correct
6 Correct 45 ms 4032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 8180 KB Output is correct
2 Correct 191 ms 8652 KB Output is correct
3 Correct 155 ms 4684 KB Output is correct
4 Correct 183 ms 8552 KB Output is correct
5 Correct 182 ms 8556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 12768 KB Output is correct
2 Correct 86 ms 19044 KB Output is correct
3 Correct 126 ms 17724 KB Output is correct
4 Correct 109 ms 20048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 320 KB Output is correct
2 Correct 91 ms 3448 KB Output is correct
3 Correct 45 ms 1380 KB Output is correct
4 Correct 45 ms 1376 KB Output is correct
5 Correct 29 ms 12612 KB Output is correct
6 Correct 58 ms 10412 KB Output is correct
7 Correct 57 ms 12940 KB Output is correct
8 Correct 38 ms 2372 KB Output is correct
9 Correct 37 ms 2260 KB Output is correct
10 Correct 39 ms 26060 KB Output is correct
11 Correct 105 ms 25336 KB Output is correct
12 Correct 33 ms 24484 KB Output is correct
13 Correct 63 ms 4704 KB Output is correct
14 Correct 33 ms 3168 KB Output is correct
15 Correct 7 ms 2988 KB Output is correct
16 Correct 8 ms 3924 KB Output is correct
17 Correct 9 ms 4220 KB Output is correct
18 Correct 45 ms 4032 KB Output is correct
19 Correct 118 ms 8180 KB Output is correct
20 Correct 191 ms 8652 KB Output is correct
21 Correct 155 ms 4684 KB Output is correct
22 Correct 183 ms 8552 KB Output is correct
23 Correct 182 ms 8556 KB Output is correct
24 Correct 157 ms 12768 KB Output is correct
25 Correct 86 ms 19044 KB Output is correct
26 Correct 126 ms 17724 KB Output is correct
27 Correct 109 ms 20048 KB Output is correct
28 Correct 122 ms 8140 KB Output is correct
29 Correct 112 ms 18060 KB Output is correct
30 Correct 56 ms 13640 KB Output is correct
31 Correct 92 ms 25384 KB Output is correct
32 Correct 183 ms 8684 KB Output is correct
33 Correct 110 ms 16068 KB Output is correct
34 Correct 119 ms 19152 KB Output is correct
35 Correct 118 ms 18996 KB Output is correct