Submission #557275

# Submission time Handle Problem Language Result Execution time Memory
557275 2022-05-05T05:16:29 Z hoanghq2004 Spring cleaning (CEOI20_cleaning) C++14
100 / 100
514 ms 18988 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1e5 + 10;

int n, q, ans;
vector <int> g[N];
int leaves;
int sz[N], big[N], in[N], head[N], hl[N], par[N], num, ti;

void dfs(int u, int p) {
    sz[u] = 1;
    par[u] = p;
    for (auto v: g[u]) {
        if (v == p) continue;
        dfs(v, u);
        sz[u] += sz[v];
        if (sz[v] >= sz[big[u]]) big[u] = v;
    }
}

void hld(int u, int p) {
    in[u] = ++ti;
    if (big[u]) {
        int v = big[u];
        hl[v] = hl[u];
        head[v] = head[u];
        hld(v, u);
    }
    for (auto v: g[u]) {
        if (v == p || v == big[u]) continue;
        hl[v] = ++num;
        head[v] = v;
        hld(v, u);
    }
}

int vis[N];
int st[N * 4], lazy[N * 4];

void push(int id, int len) {
    lazy[id] ^= 1;
    st[id] = len - st[id];
}

void modify(int id, int L, int R, int u, int v) {
    if (u > R || L > v) return;
    if (u <= L && R <= v) {
        push(id, R - L + 1);
        return;
    }
    int mid = L + R >> 1;
    if (lazy[id]) {
        push(id * 2, mid - L + 1);
        push(id * 2 + 1, R - mid);
        lazy[id] = 0;
    }
    modify(id * 2, L, mid, u, v);
    modify(id * 2 + 1, mid + 1, R, u, v);
    st[id] = st[id * 2] + st[id * 2 + 1];
}

void solve() {
    ++ti;
    int m;
    cin >> m;
    vector <int> s, rev;
    int l = leaves + m;
    while (m--) {
        int u;
        cin >> u;
        if (g[u].size() <= 1 && vis[u] != ti) {
            --l;
            vis[u] = ti;
            rev.push_back(u);
        }
        s.push_back(u);
    }
    if (l % 2) {
        cout << -1 << '\n';
        return;
    }
    for (auto u: rev)
        for (; u; u = par[head[u]]) modify(1, 1, n, in[head[u]], in[u]);
    for (auto u: s)
        for (; u; u = par[head[u]]) modify(1, 1, n, in[head[u]], in[u]);
    int _1 = st[1] + s.size();
    int _2 = (n - 1 - st[1]);
    cout << _1 + _2 * 2 << '\n';
    for (auto u: s)
        for (; u; u = par[head[u]]) modify(1, 1, n, in[head[u]], in[u]);
    for (auto u: rev)
        for (; u; u = par[head[u]]) modify(1, 1, n, in[head[u]], in[u]);
}

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> n >> q;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int r = 1;
    for (int i = 1; i <= n; ++i)
        if (g[i].size() > 1) r = i;

    dfs(r, 0);
    hl[r] = ++num;
    head[r] = r;
    hld(r, 0);
    for (int i = 1; i <= n; ++i)
        if (g[i].size() <= 1) {
            ++leaves;
            for (int u = i; u; u = par[head[u]]) modify(1, 1, n, in[head[u]], in[u]);
        }
    while (q--) solve();
}

Compilation message

cleaning.cpp: In function 'void modify(int, int, int, int, int)':
cleaning.cpp:61:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   61 |     int mid = L + R >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 210 ms 5544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 3956 KB Output is correct
2 Correct 9 ms 3956 KB Output is correct
3 Correct 73 ms 11204 KB Output is correct
4 Correct 142 ms 10816 KB Output is correct
5 Correct 192 ms 12836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 4480 KB Output is correct
2 Correct 9 ms 4440 KB Output is correct
3 Correct 47 ms 18988 KB Output is correct
4 Correct 88 ms 18624 KB Output is correct
5 Correct 37 ms 16664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 5548 KB Output is correct
2 Correct 86 ms 4816 KB Output is correct
3 Correct 19 ms 4584 KB Output is correct
4 Correct 18 ms 5048 KB Output is correct
5 Correct 17 ms 4988 KB Output is correct
6 Correct 74 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 235 ms 9092 KB Output is correct
2 Correct 514 ms 8940 KB Output is correct
3 Correct 367 ms 6164 KB Output is correct
4 Correct 485 ms 8928 KB Output is correct
5 Correct 480 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 13084 KB Output is correct
2 Correct 159 ms 15808 KB Output is correct
3 Correct 146 ms 15228 KB Output is correct
4 Correct 151 ms 15648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 210 ms 5544 KB Output is correct
3 Correct 43 ms 3956 KB Output is correct
4 Correct 9 ms 3956 KB Output is correct
5 Correct 73 ms 11204 KB Output is correct
6 Correct 142 ms 10816 KB Output is correct
7 Correct 192 ms 12836 KB Output is correct
8 Correct 43 ms 4480 KB Output is correct
9 Correct 9 ms 4440 KB Output is correct
10 Correct 47 ms 18988 KB Output is correct
11 Correct 88 ms 18624 KB Output is correct
12 Correct 37 ms 16664 KB Output is correct
13 Correct 103 ms 5548 KB Output is correct
14 Correct 86 ms 4816 KB Output is correct
15 Correct 19 ms 4584 KB Output is correct
16 Correct 18 ms 5048 KB Output is correct
17 Correct 17 ms 4988 KB Output is correct
18 Correct 74 ms 5376 KB Output is correct
19 Correct 235 ms 9092 KB Output is correct
20 Correct 514 ms 8940 KB Output is correct
21 Correct 367 ms 6164 KB Output is correct
22 Correct 485 ms 8928 KB Output is correct
23 Correct 480 ms 8952 KB Output is correct
24 Correct 237 ms 13084 KB Output is correct
25 Correct 159 ms 15808 KB Output is correct
26 Correct 146 ms 15228 KB Output is correct
27 Correct 151 ms 15648 KB Output is correct
28 Correct 338 ms 8628 KB Output is correct
29 Correct 189 ms 14688 KB Output is correct
30 Correct 79 ms 12740 KB Output is correct
31 Correct 83 ms 18708 KB Output is correct
32 Correct 470 ms 9052 KB Output is correct
33 Correct 164 ms 14028 KB Output is correct
34 Correct 220 ms 14584 KB Output is correct
35 Correct 223 ms 15396 KB Output is correct