답안 #830067

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
830067 2023-08-18T18:20:14 Z t6twotwo Spring cleaning (CEOI20_cleaning) C++17
100 / 100
173 ms 19004 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int M, leaves, timer;
vector<bool> leaf;
vector<int> f, par, sz, in, top, c0, c1, lz;
vector<vector<int>> adj;
void init(int x) {
    if (adj[x].size() == 1) {
        f[x] = 1;
        leaf[x] = 1;
        leaves ^= 1;
    }
    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;
        init(y);
        f[x] ^= f[y];
        sz[x] += sz[y];
        if (sz[y] > sz[adj[x][0]]) {
            swap(y, adj[x][0]);
        }
    }
}
void dfs(int x) {
    in[x] = timer++;
    for (int y : adj[x]) {
        top[y] = adj[x][0] == y ? top[x] : y;
        dfs(y);
    }
}
void apply(int p, int v) {
    if (v == 0) {
        return;
    }
    swap(c0[p], c1[p]);
    lz[p] ^= 1;
}
void push(int p) {
    apply(p * 2, lz[p]);
    apply(p * 2 + 1, lz[p]);
    lz[p] = 0;
}
void pull(int p) {
    c0[p] = c0[p * 2] + c0[p * 2 + 1];
    c1[p] = c1[p * 2] + c1[p * 2 + 1];
}
void upd(int p, int l, int r, int L, int R) {
    if (R <= l || r <= L) {
        return;
    }
    if (L <= l && r <= R) {
        apply(p, 1);
        return;
    }
    int m = (l + r + 1) / 2;
    push(p);
    upd(p * 2, l, m, L, R);
    upd(p * 2 + 1, m, r, L, R);
    pull(p);
}
void build(vector<int> &a) {
    int N = a.size();
    M = 2 << __lg(N);
    c0.resize(2 * M);
    c1.resize(2 * M);
    lz.resize(2 * M);
    for (int i = 0; i < N; i++) {
        if (a[i] == 0) {
            c0[in[i] + M] = 1;
        } else {
            c1[in[i] + M] = 1;
        }
    }
    for (int i = M - 1; i; i--) {
        pull(i);
    }
}
void update(int s) {
    while (s != -1) {
        upd(1, 0, M, in[top[s]], in[s] + 1);
        s = par[top[s]];
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, Q;
    cin >> N >> Q;
    adj.resize(N);
    for (int i = 0; i < N - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    f.resize(N);
    in.resize(N);
    sz.resize(N);
    top.resize(N);
    par.resize(N, -1);
    leaf.resize(N);
    init(0);
    dfs(0);
    build(f);
    auto l = leaf;
    while (Q--) {
        int D;
        cin >> D;
        vector<int> p(D);
        for (int i = 0; i < D; i++) {
            cin >> p[i];
            p[i]--;
            if (l[p[i]]) {
                l[p[i]] = 0;
                leaves ^= 1;
            } else {
                update(p[i]);
            }
        }
        leaves ^= D & 1;
        if (leaves == 1) {
            cout << -1 << "\n";
        } else {
            cout << N + D + c0[1] - 2 << "\n";
        }
        leaves ^= D & 1;
        for (int i = 0; i < D; i++) {
            if (leaf[p[i]] && !l[p[i]]) {
                l[p[i]] = 1;
                leaves ^= 1;
            } else {
                update(p[i]);
            }
        }
    }
    return 6/22;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 95 ms 2656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 980 KB Output is correct
2 Correct 50 ms 980 KB Output is correct
3 Correct 23 ms 11176 KB Output is correct
4 Correct 53 ms 9272 KB Output is correct
5 Correct 56 ms 11512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 1472 KB Output is correct
2 Correct 39 ms 1492 KB Output is correct
3 Correct 41 ms 18448 KB Output is correct
4 Correct 92 ms 17516 KB Output is correct
5 Correct 29 ms 17092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 3660 KB Output is correct
2 Correct 45 ms 3020 KB Output is correct
3 Correct 9 ms 2760 KB Output is correct
4 Correct 7 ms 3272 KB Output is correct
5 Correct 9 ms 3480 KB Output is correct
6 Correct 45 ms 3532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 7048 KB Output is correct
2 Correct 169 ms 6788 KB Output is correct
3 Correct 138 ms 3564 KB Output is correct
4 Correct 170 ms 6792 KB Output is correct
5 Correct 171 ms 6804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 167 ms 11468 KB Output is correct
2 Correct 79 ms 14600 KB Output is correct
3 Correct 140 ms 15424 KB Output is correct
4 Correct 100 ms 16568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 95 ms 2656 KB Output is correct
3 Correct 66 ms 980 KB Output is correct
4 Correct 50 ms 980 KB Output is correct
5 Correct 23 ms 11176 KB Output is correct
6 Correct 53 ms 9272 KB Output is correct
7 Correct 56 ms 11512 KB Output is correct
8 Correct 36 ms 1472 KB Output is correct
9 Correct 39 ms 1492 KB Output is correct
10 Correct 41 ms 18448 KB Output is correct
11 Correct 92 ms 17516 KB Output is correct
12 Correct 29 ms 17092 KB Output is correct
13 Correct 62 ms 3660 KB Output is correct
14 Correct 45 ms 3020 KB Output is correct
15 Correct 9 ms 2760 KB Output is correct
16 Correct 7 ms 3272 KB Output is correct
17 Correct 9 ms 3480 KB Output is correct
18 Correct 45 ms 3532 KB Output is correct
19 Correct 119 ms 7048 KB Output is correct
20 Correct 169 ms 6788 KB Output is correct
21 Correct 138 ms 3564 KB Output is correct
22 Correct 170 ms 6792 KB Output is correct
23 Correct 171 ms 6804 KB Output is correct
24 Correct 167 ms 11468 KB Output is correct
25 Correct 79 ms 14600 KB Output is correct
26 Correct 140 ms 15424 KB Output is correct
27 Correct 100 ms 16568 KB Output is correct
28 Correct 116 ms 7628 KB Output is correct
29 Correct 110 ms 15272 KB Output is correct
30 Correct 54 ms 12860 KB Output is correct
31 Correct 92 ms 19004 KB Output is correct
32 Correct 173 ms 8152 KB Output is correct
33 Correct 115 ms 13440 KB Output is correct
34 Correct 114 ms 15820 KB Output is correct
35 Correct 120 ms 15760 KB Output is correct