답안 #1098306

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098306 2024-10-09T08:54:41 Z Zero_OP Spring cleaning (CEOI20_cleaning) C++14
100 / 100
166 ms 20176 KB
#include <bits/stdc++.h>

using namespace std;

#define dbg(x) "[" #x " = " << (x) << "]"

const int MAX = 1e5 + 5;

int n, q, timerHLD, root, parent[MAX], heavy[MAX], sz[MAX], depth[MAX], head[MAX], posHLD[MAX];
vector<int> adj[MAX];
bool original_is_leaf[MAX], is_leaf[MAX];

void predfs(int u, int pre){
    sz[u] = 1;
    for(int v : adj[u]) if(v != pre){
        depth[v] = depth[u] + 1;
        parent[v] = u;
        predfs(v, u);
        sz[u] += sz[v];
        if(heavy[u] == 0 || sz[heavy[u]] < sz[v]){
            heavy[u] = v;
        }
    }
}

void dfsHLD(int u, int hd){
    head[u] = hd;
    posHLD[u] = ++timerHLD;
    if(heavy[u] != 0){
        dfsHLD(heavy[u], hd);
        for(int v : adj[u]){
            if(v != parent[u] && v != heavy[u]){
                dfsHLD(v, v);
            }
        }
    }
}

struct SegmentTree{
    vector<int> st, lazy;

    SegmentTree(int n) : st(n << 2), lazy(n << 2) {}

    void build(int id, int l, int r){
        st[id] = r - l + 1;
        if(l < r){
            int mid = l + r >> 1;
            build(id << 1, l, mid);
            build(id << 1 | 1, mid + 1, r);
            st[id] = st[id << 1] + st[id << 1 | 1];
        }
    }

    void flip(int id, int l, int r){
        st[id] = (r - l + 1) - st[id];
        lazy[id] ^= 1;
    }

    void lazyDown(int id, int l, int r, int mid){
        if(lazy[id] > 0){
            flip(id << 1, l, mid);
            flip(id << 1 | 1, mid + 1, r);
            lazy[id] = 0;
        }
    }

    void pull(int id){
        st[id] = st[id << 1] + st[id << 1 | 1];
    }

    void update(int id, int l, int r, int u, int v){
        if(u <= l && r <= v){
            flip(id, l, r);
        } else{
            int mid = l + r >> 1;
            lazyDown(id, l, r, mid);
            if(u <= mid) update(id << 1, l, mid, u, v);
            if(mid < v) update(id << 1 | 1, mid + 1, r, u, v);
            pull(id);
        }
    }

    int getFilled(){
        return st[1];
    }
};

void toggle(int u, SegmentTree& T){
    while(u > 0){
        T.update(1, 1, n, posHLD[head[u]], posHLD[u]);
        u = parent[head[u]];
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    #define filename "task"
    if(fopen(filename".inp", "r")){
        freopen(filename".inp", "r", stdin);
        freopen(filename".out", "w", stdout);
    }

    cin >> n >> q;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for(int i = 1; i <= n; ++i){
        if((int)adj[i].size() > 1){
            root = i;
            break;
        }
    }

    predfs(root, -1);
    dfsHLD(root, root);
    SegmentTree T(n);
    T.build(1, 1, n);

    int numLeaves = 0;
    for(int i = 1; i <= n; ++i){
        if((int)adj[i].size() == 1){
            original_is_leaf[i] = true;
            ++numLeaves;
            toggle(i, T);
        }
    }

    copy(original_is_leaf + 1, original_is_leaf + 1 + n, is_leaf + 1);
    int original_numLeaves = numLeaves;

    while(q--){
        vector<int> roll;
        int D;
        cin >> D;
        vector<int> nodes(D);
        for(int i = 0; i < D; ++i){
            cin >> nodes[i];
            if(is_leaf[nodes[i]]){
                is_leaf[nodes[i]] = false;
            } else{
                toggle(nodes[i], T);
                ++numLeaves;
                roll.push_back(nodes[i]);
            }
        }

        if(numLeaves & 1){
            cout << -1 << '\n';
        } else{
            cout << n + D + T.getFilled() - 2 << '\n';
        }

        numLeaves = original_numLeaves;
        for(int u : nodes){
            is_leaf[u] = original_is_leaf[u];
        }

        while(!roll.empty()){
            toggle(roll.back(), T);
            roll.pop_back();
        }
    }
}

Compilation message

cleaning.cpp: In member function 'void SegmentTree::build(int, int, int)':
cleaning.cpp:47:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |             int mid = l + r >> 1;
      |                       ~~^~~
cleaning.cpp: In member function 'void SegmentTree::update(int, int, int, int, int)':
cleaning.cpp:75:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |             int mid = l + r >> 1;
      |                       ~~^~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen(filename".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         freopen(filename".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 67 ms 4704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 4056 KB Output is correct
2 Correct 37 ms 4056 KB Output is correct
3 Correct 44 ms 11464 KB Output is correct
4 Correct 59 ms 9688 KB Output is correct
5 Correct 68 ms 11980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 4568 KB Output is correct
2 Correct 30 ms 4532 KB Output is correct
3 Correct 32 ms 19028 KB Output is correct
4 Correct 71 ms 18640 KB Output is correct
5 Correct 26 ms 17496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 5612 KB Output is correct
2 Correct 27 ms 4956 KB Output is correct
3 Correct 12 ms 4844 KB Output is correct
4 Correct 9 ms 5212 KB Output is correct
5 Correct 11 ms 5560 KB Output is correct
6 Correct 35 ms 5624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 8776 KB Output is correct
2 Correct 162 ms 8532 KB Output is correct
3 Correct 121 ms 5720 KB Output is correct
4 Correct 166 ms 8500 KB Output is correct
5 Correct 165 ms 8632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 11980 KB Output is correct
2 Correct 88 ms 15440 KB Output is correct
3 Correct 98 ms 16676 KB Output is correct
4 Correct 98 ms 16156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 67 ms 4704 KB Output is correct
3 Correct 31 ms 4056 KB Output is correct
4 Correct 37 ms 4056 KB Output is correct
5 Correct 44 ms 11464 KB Output is correct
6 Correct 59 ms 9688 KB Output is correct
7 Correct 68 ms 11980 KB Output is correct
8 Correct 29 ms 4568 KB Output is correct
9 Correct 30 ms 4532 KB Output is correct
10 Correct 32 ms 19028 KB Output is correct
11 Correct 71 ms 18640 KB Output is correct
12 Correct 26 ms 17496 KB Output is correct
13 Correct 49 ms 5612 KB Output is correct
14 Correct 27 ms 4956 KB Output is correct
15 Correct 12 ms 4844 KB Output is correct
16 Correct 9 ms 5212 KB Output is correct
17 Correct 11 ms 5560 KB Output is correct
18 Correct 35 ms 5624 KB Output is correct
19 Correct 111 ms 8776 KB Output is correct
20 Correct 162 ms 8532 KB Output is correct
21 Correct 121 ms 5720 KB Output is correct
22 Correct 166 ms 8500 KB Output is correct
23 Correct 165 ms 8632 KB Output is correct
24 Correct 148 ms 11980 KB Output is correct
25 Correct 88 ms 15440 KB Output is correct
26 Correct 98 ms 16676 KB Output is correct
27 Correct 98 ms 16156 KB Output is correct
28 Correct 108 ms 9296 KB Output is correct
29 Correct 99 ms 16468 KB Output is correct
30 Correct 71 ms 13260 KB Output is correct
31 Correct 70 ms 20176 KB Output is correct
32 Correct 160 ms 9992 KB Output is correct
33 Correct 93 ms 13740 KB Output is correct
34 Correct 107 ms 16468 KB Output is correct
35 Correct 103 ms 16524 KB Output is correct