Submission #313049

# Submission time Handle Problem Language Result Execution time Memory
313049 2020-10-15T06:14:05 Z jhnah917 Spring cleaning (CEOI20_cleaning) C++14
100 / 100
393 ms 27632 KB
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef long long ll;

int tree[1 << 18], tmp[1 << 18];
void push(int node, int s, int e){
    if(!tmp[node]) return;
    tree[node] = (e-s+1) - tree[node];
    if(s != e){
        tmp[node << 1] ^= 1;
        tmp[node << 1 | 1] ^= 1;
    }
    tmp[node] = 0;
}
void update(int node, int s, int e, int l, int r){
    push(node, s, e);
    if(r < s || e < l) return;
    if(l <= s && e <= r){ tmp[node] ^= 1; push(node, s, e); return; }
    int m = s + e >> 1;
    update(node << 1, s, m, l, r);
    update(node << 1 | 1, m+1, e, l, r);
    tree[node] = tree[node << 1] + tree[node << 1 | 1];
}
int query(int node, int s, int e, int l, int r){
    push(node, s, e);
    if(r < s || e < l) return 0;
    if(l <= s && e <= r) return tree[node];
    int m = s + e >> 1;
    return query(node << 1, s, m, l, r) + query(node << 1 | 1, m+1, e, l, r);
}

int n, q, leaf[101010];
vector<int> inp[101010], g[101010];
int top[101010], dep[101010], par[101010], sz[101010], in[101010], pv;
void dfs(int v, int b){
    for(auto i : inp[v]) if(i != b) {
        g[v].push_back(i);
        dep[i] = dep[v] + 1; par[i] = v;
        dfs(i, v);
    }
}
void dfs1(int v){
    sz[v] = 1;
    for(auto &i : g[v]){
        dfs1(i); sz[v] += sz[i];
        if(sz[i] > sz[g[v][0]]) swap(i, g[v][0]);
    }
}
void dfs2(int v){
    in[v] = ++pv;
    for(auto i : g[v]){
        top[i] = i == g[v][0] ? top[v] : i;
        dfs2(i);
    }
}
void updatePath(int s, int e){
    for(; top[s]!=top[e]; s=par[top[s]]){
        if(dep[top[s]] < dep[top[e]]) swap(s, e);
        update(1, 1, n, in[top[s]], in[s]);
    }
    if(dep[s] > dep[e]) swap(s, e);
    update(1, 1, n, in[s], in[e]);
}
int getAns(int cnt){
    if(query(1, 1, n, 1, 1)) return -1;
    return 2*(n-1) - query(1, 1, n, 1, n) + cnt;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> q;
    for(int i=1; i<n; i++){
        int s, e; cin >> s >> e;
        inp[s].push_back(e); inp[e].push_back(s);
    }
    top[1] = 1; dfs(1, -1); dfs1(1); dfs2(1);
    for(int i=1; i<=n; i++) if(inp[i].size() == 1) leaf[i] = 1, updatePath(1, i);
    while(q--){
        int cnt; cin >> cnt;
        vector<int> add; set<int> st;
        for(int i=0; i<cnt; i++){
            int t; cin >> t;
            if(!leaf[t] || st.count(t)) add.push_back(t);
            st.insert(t);
        }
        for(auto i : add) updatePath(1, i);
        cout << getAns(cnt) << "\n";
        for(auto i : add) updatePath(1, i);
    }
}

Compilation message

cleaning.cpp: In function 'void update(int, int, int, int, int)':
cleaning.cpp:24:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   24 |     int m = s + e >> 1;
      |             ~~^~~
cleaning.cpp: In function 'int query(int, int, int, int, int)':
cleaning.cpp:33:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |     int m = s + e >> 1;
      |             ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 156 ms 8056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 6432 KB Output is correct
2 Correct 84 ms 6552 KB Output is correct
3 Correct 102 ms 14188 KB Output is correct
4 Correct 157 ms 14960 KB Output is correct
5 Correct 185 ms 17384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 7164 KB Output is correct
2 Correct 70 ms 7284 KB Output is correct
3 Correct 78 ms 25848 KB Output is correct
4 Correct 185 ms 27632 KB Output is correct
5 Correct 65 ms 23032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 8440 KB Output is correct
2 Correct 69 ms 7812 KB Output is correct
3 Correct 27 ms 7288 KB Output is correct
4 Correct 21 ms 8064 KB Output is correct
5 Correct 24 ms 8184 KB Output is correct
6 Correct 80 ms 8404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 300 ms 11384 KB Output is correct
2 Correct 385 ms 12280 KB Output is correct
3 Correct 276 ms 9204 KB Output is correct
4 Correct 383 ms 12280 KB Output is correct
5 Correct 388 ms 12284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 393 ms 15420 KB Output is correct
2 Correct 227 ms 19960 KB Output is correct
3 Correct 276 ms 20088 KB Output is correct
4 Correct 254 ms 21368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 156 ms 8056 KB Output is correct
3 Correct 81 ms 6432 KB Output is correct
4 Correct 84 ms 6552 KB Output is correct
5 Correct 102 ms 14188 KB Output is correct
6 Correct 157 ms 14960 KB Output is correct
7 Correct 185 ms 17384 KB Output is correct
8 Correct 67 ms 7164 KB Output is correct
9 Correct 70 ms 7284 KB Output is correct
10 Correct 78 ms 25848 KB Output is correct
11 Correct 185 ms 27632 KB Output is correct
12 Correct 65 ms 23032 KB Output is correct
13 Correct 112 ms 8440 KB Output is correct
14 Correct 69 ms 7812 KB Output is correct
15 Correct 27 ms 7288 KB Output is correct
16 Correct 21 ms 8064 KB Output is correct
17 Correct 24 ms 8184 KB Output is correct
18 Correct 80 ms 8404 KB Output is correct
19 Correct 300 ms 11384 KB Output is correct
20 Correct 385 ms 12280 KB Output is correct
21 Correct 276 ms 9204 KB Output is correct
22 Correct 383 ms 12280 KB Output is correct
23 Correct 388 ms 12284 KB Output is correct
24 Correct 393 ms 15420 KB Output is correct
25 Correct 227 ms 19960 KB Output is correct
26 Correct 276 ms 20088 KB Output is correct
27 Correct 254 ms 21368 KB Output is correct
28 Correct 271 ms 12024 KB Output is correct
29 Correct 255 ms 19064 KB Output is correct
30 Correct 186 ms 17524 KB Output is correct
31 Correct 188 ms 27632 KB Output is correct
32 Correct 386 ms 12284 KB Output is correct
33 Correct 239 ms 18288 KB Output is correct
34 Correct 278 ms 20856 KB Output is correct
35 Correct 276 ms 20832 KB Output is correct