답안 #342463

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
342463 2021-01-02T07:15:21 Z mjhmjh1104 Spring cleaning (CEOI20_cleaning) C++14
100 / 100
883 ms 27612 KB
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 100006;

long long tree1[262144]; /// mod 2
long long lazy1[262144];

void propagate1(int i, int b, int e) {
    if (!lazy1[i]) return;
    if (b != e) {
        lazy1[i * 2 + 1] += lazy1[i];
        lazy1[i * 2 + 2] += lazy1[i];
    }
    if (lazy1[i] % 2) tree1[i] = (e - b + 1) - tree1[i];
    lazy1[i] = 0;
}

long long update1(int i, int b, int e, int l, int r, int v) {
    propagate1(i, b, e);
    if (r < b || e < l) return tree1[i];
    if (l <= b && e <= r) {
        lazy1[i] += v;
        propagate1(i, b, e);
        return tree1[i];
    }
    int m = (b + e) / 2;
    return tree1[i] = update1(i * 2 + 1, b, m, l, r, v) + update1(i * 2 + 2, m + 1, e, l, r, v);
}

long long tree2[262144]; /// more than 0
long long lazy2[262144];

void update2(int i, int b, int e, int l, int r, int v) {
    if (r < b || e < l) return;
    int m = (b + e) / 2;
    if (l <= b && e <= r) lazy2[i] += v;
    else update2(i * 2 + 1, b, m, l, r, v), update2(i * 2 + 2, m + 1, e, l, r, v);
    if (lazy2[i]) tree2[i] = e - b + 1;
    else if (b == e) tree2[i] = 0;
    else tree2[i] = tree2[i * 2 + 1] + tree2[i * 2 + 2];
}

void update(int l, int r, int v) {
    update1(0, 0, 131071, l, r, v);
    update2(0, 0, 131071, l, r, v);
}

int sz[MAX], in[MAX], top[MAX], t;
int n, q, d, childs[MAX], depth[MAX], par[MAX];
bool alreadyLeaf[MAX];
vector<int> adj[MAX], c[MAX];

int dfs_sz(int x) {
    sz[x] = 1;
    for (auto &i: c[x]) {
        sz[x] += dfs_sz(i);
        if (sz[i] > sz[c[x][0]]) swap(i, c[x][0]);
    }
    return sz[x];
}

void dfs_hld(int x) {
    in[x] = t++;
    for (auto &i: c[x]) {
        top[i] = (i == c[x][0] ? top[x] : i);
        dfs_hld(i);
    }
}


void dfs(int x, int prev = -1) {
    par[x] = prev;
    for (auto &i: adj[x]) if (i != prev) {
        depth[i] = depth[x] + 1;
        dfs(i, x);
        c[x].push_back(i);
    }
}

void update_hld(int x, int y, int v) {
    while (top[x] != top[y]) {
        if (depth[top[x]] < depth[top[y]]) swap(x, y);
        update(in[top[x]], in[x], v);
        x = par[top[x]];
    }
    if (in[x] > in[y]) swap(x, y);
    update(in[x], in[y], v);
}

int main() {
    scanf("%d%d", &n, &q);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(0);
    dfs_sz(0);
    dfs_hld(0);
    int r = 0;
    for (int i = 0; i < n; i++) {
        if (i == 0 && (int)c[0].size() != 1) continue;
        if (i != 0 && !c[i].empty()) continue;
        update_hld(0, i, 1);
        alreadyLeaf[i] = true;
        r++;
    }
    int _or = r;
    while (q--) {
        r = _or;
        scanf("%d", &d);
        vector<int> x;
        for (int i = 0; i < d; i++) {
            int t;
            scanf("%d", &t); t--;
            x.push_back(t);
            childs[t]++;
            if (alreadyLeaf[t] && childs[t] == 1) continue;
            update_hld(0, t, 1), r++;
        }
        if (r % 2) {
            puts("-1");
            goto next;
        }
        {
            propagate1(0, 0, 131071);
            long long first = tree1[0];
            long long second = tree2[0];
            if (r > 0) second--;
            printf("%lld\n", 2 * second - first + d);
        }

    next:
        for (int i = (int)x.size() - 1; i >= 0; i--) {
            childs[x[i]]--;
            if (alreadyLeaf[x[i]] && !childs[x[i]]) continue;
            update_hld(0, x[i], -1);
        }
    }
}

Compilation message

cleaning.cpp: In function 'int main()':
cleaning.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   94 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
cleaning.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   97 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
cleaning.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  116 |         scanf("%d", &d);
      |         ~~~~~^~~~~~~~~~
cleaning.cpp:120:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  120 |             scanf("%d", &t); t--;
      |             ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 351 ms 8044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 6268 KB Output is correct
2 Correct 198 ms 6268 KB Output is correct
3 Correct 183 ms 16232 KB Output is correct
4 Correct 254 ms 13668 KB Output is correct
5 Correct 296 ms 17000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 7020 KB Output is correct
2 Correct 149 ms 7016 KB Output is correct
3 Correct 81 ms 27612 KB Output is correct
4 Correct 309 ms 26236 KB Output is correct
5 Correct 67 ms 22892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 230 ms 8556 KB Output is correct
2 Correct 137 ms 8044 KB Output is correct
3 Correct 49 ms 7840 KB Output is correct
4 Correct 30 ms 8556 KB Output is correct
5 Correct 37 ms 8556 KB Output is correct
6 Correct 162 ms 8684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 598 ms 14136 KB Output is correct
2 Correct 863 ms 13932 KB Output is correct
3 Correct 632 ms 9580 KB Output is correct
4 Correct 831 ms 13804 KB Output is correct
5 Correct 881 ms 13832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 761 ms 19172 KB Output is correct
2 Correct 357 ms 21100 KB Output is correct
3 Correct 470 ms 21996 KB Output is correct
4 Correct 400 ms 23156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 351 ms 8044 KB Output is correct
3 Correct 200 ms 6268 KB Output is correct
4 Correct 198 ms 6268 KB Output is correct
5 Correct 183 ms 16232 KB Output is correct
6 Correct 254 ms 13668 KB Output is correct
7 Correct 296 ms 17000 KB Output is correct
8 Correct 147 ms 7020 KB Output is correct
9 Correct 149 ms 7016 KB Output is correct
10 Correct 81 ms 27612 KB Output is correct
11 Correct 309 ms 26236 KB Output is correct
12 Correct 67 ms 22892 KB Output is correct
13 Correct 230 ms 8556 KB Output is correct
14 Correct 137 ms 8044 KB Output is correct
15 Correct 49 ms 7840 KB Output is correct
16 Correct 30 ms 8556 KB Output is correct
17 Correct 37 ms 8556 KB Output is correct
18 Correct 162 ms 8684 KB Output is correct
19 Correct 598 ms 14136 KB Output is correct
20 Correct 863 ms 13932 KB Output is correct
21 Correct 632 ms 9580 KB Output is correct
22 Correct 831 ms 13804 KB Output is correct
23 Correct 881 ms 13832 KB Output is correct
24 Correct 761 ms 19172 KB Output is correct
25 Correct 357 ms 21100 KB Output is correct
26 Correct 470 ms 21996 KB Output is correct
27 Correct 400 ms 23156 KB Output is correct
28 Correct 563 ms 13548 KB Output is correct
29 Correct 482 ms 20588 KB Output is correct
30 Correct 305 ms 17000 KB Output is correct
31 Correct 327 ms 26308 KB Output is correct
32 Correct 883 ms 13872 KB Output is correct
33 Correct 444 ms 19156 KB Output is correct
34 Correct 556 ms 22636 KB Output is correct
35 Correct 516 ms 22764 KB Output is correct