Submission #878181

# Submission time Handle Problem Language Result Execution time Memory
878181 2023-11-24T06:48:55 Z Alish Spring cleaning (CEOI20_cleaning) C++17
100 / 100
459 ms 23268 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long int	ll;
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;


#define F		        first
#define S		        second
#define pb		        push_back
#define endl            '\n'
#define Mp		        make_pair
#define all(x)          x.begin(), x.end()
#define debug(x)        cerr << #x << " = " << x << endl;
#define fast_io         ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io         freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);


ll mod = 1e9+7 ;


const int N = 1e5+23;

vector<int> g[N];
int par[N], head[N], h[N], sz[N], bi[N], pos[N]; // hld
int seg[4*N], lpz[4*N], st[N], ft[N], tim;//segment
int deg[N];
int n, q;
int is[N];

void DFS(int v, int p=0){
    int Mx=0;
    sz[v]=1;
    for (int u: g[v]){
        if(u==p) continue;
        h[u]=h[v]+1;
        par[u]=v;
        DFS(u, v);
        if(sz[u]>Mx){
            bi[v]=u;
            Mx=sz[u];
        }
        sz[v]+=sz[u];
        is[v]+=is[u];
    }
}

void dfs(int v, int p=0){
    st[v]=tim;
    pos[st[v]]=v;
    tim++;
    if(!p) head[v]=v;
    if(bi[v]){
        head[bi[v]]=head[v];
        dfs(bi[v], v);
    }
    for (int u: g[v]){
        if(u==p || u==bi[v]) continue;
        head[u]=u;
        dfs(u, v);
    }
    ft[v]=tim;
}

void Shift(int l, int r, int ind){
    if(r-l==1) return;
    if(!lpz[ind]) return ;
    int mid=(r+l)>>1;
    seg[2*ind+1]=mid-l-seg[2*ind+1];
    seg[2*ind+2]=r-mid-seg[2*ind+2];
    lpz[2*ind+1]^=1;
    lpz[2*ind+2]^=1;
    lpz[ind]=0;
}
void build(int l=0, int r=n, int ind=0){
    if(r-l==1){
        if(is[pos[l]]%2==0) seg[ind]=1;
        return;
    }
    int mid=(r+l)>>1;
    build(l, mid, 2*ind+1); build(mid, r, 2*ind+2);
    seg[ind]=seg[2*ind+1]+seg[2*ind+2];
}

void upd(int lx, int rx, int l=0, int r=n, int ind=0){
    Shift(l, r, ind);
    if(lx>=r || rx<=l) return;
    if(lx<=l && rx>=r){
        seg[ind]=r-l-seg[ind];
        lpz[ind]^=1;
        return;
    }
    int mid=(r+l)>>1;
    upd(lx, rx, l, mid, 2*ind+1); upd(lx, rx, mid, r, 2*ind+2);
    seg[ind]=seg[2*ind+1]+seg[2*ind+2];
}

void updd(int v, int u){
    while(head[u]!=head[v]){
        if(h[head[u]] < h[head[v]]) swap(u, v);
        upd(st[head[u]], st[u]+1);
        u=par[head[u]];
    }
    if(h[v]>h[u]) swap(u, v);
    upd(st[v], st[u]+1);
}

int get(int i, int l=0, int r=n, int ind=0){
    Shift(l, r, ind);
    if(r-l==1) return seg[ind];
    int mid=(r+l)>>1;
    if(i<mid) return get(i, l, mid, 2*ind+1);
    return get(i, mid, r, 2*ind+2);
}


int main()
{
    fast_io
    cin>>n>>q;
    for (int i=0; i<n-1; i++){
        int u, v; cin>>v>>u;
        g[v].pb(u); g[u].pb(v);
        deg[v]++; deg[u]++;
    }
    int r=0;
    int leaf=0;
    for (int i=1; i<=n; i++){
        if(g[i].size()==1) is[i]=1;
        else r=i;
    }
    DFS(r); dfs(r);
    build();


    while(q--){
        vector<int> d;
        int k; cin>>k;
        for (int i=0; i<k; i++) {
            int p; cin>>p;
            d.pb(p);
        }
        for (auto pp: d) {
                if(deg[pp]==1) updd(r, pp);
                deg[pp]++;
        }
        for (auto pp: d) updd(r, pp);
        if(!get(0))cout<<-1<<endl;

        else cout<<n+k-1+seg[0]-1<<endl;
        for (auto pp: d) {
            deg[pp]--;
            if(deg[pp]==1)  updd(r, pp);
        }

        for (auto pp: d) updd(r, pp);
    }
}

Compilation message

cleaning.cpp: In function 'int main()':
cleaning.cpp:129:9: warning: unused variable 'leaf' [-Wunused-variable]
  129 |     int leaf=0;
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 209 ms 8648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 8412 KB Output is correct
2 Correct 43 ms 8760 KB Output is correct
3 Correct 32 ms 13536 KB Output is correct
4 Correct 128 ms 13192 KB Output is correct
5 Correct 136 ms 14484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 10976 KB Output is correct
2 Correct 55 ms 10976 KB Output is correct
3 Correct 36 ms 22028 KB Output is correct
4 Correct 93 ms 21728 KB Output is correct
5 Correct 30 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 10964 KB Output is correct
2 Correct 79 ms 8796 KB Output is correct
3 Correct 10 ms 10384 KB Output is correct
4 Correct 8 ms 11216 KB Output is correct
5 Correct 15 ms 11212 KB Output is correct
6 Correct 70 ms 11568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 251 ms 12156 KB Output is correct
2 Correct 459 ms 11940 KB Output is correct
3 Correct 365 ms 11600 KB Output is correct
4 Correct 421 ms 13156 KB Output is correct
5 Correct 422 ms 13140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 13392 KB Output is correct
2 Correct 153 ms 16876 KB Output is correct
3 Correct 166 ms 16072 KB Output is correct
4 Correct 187 ms 16632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 209 ms 8648 KB Output is correct
3 Correct 49 ms 8412 KB Output is correct
4 Correct 43 ms 8760 KB Output is correct
5 Correct 32 ms 13536 KB Output is correct
6 Correct 128 ms 13192 KB Output is correct
7 Correct 136 ms 14484 KB Output is correct
8 Correct 43 ms 10976 KB Output is correct
9 Correct 55 ms 10976 KB Output is correct
10 Correct 36 ms 22028 KB Output is correct
11 Correct 93 ms 21728 KB Output is correct
12 Correct 30 ms 21084 KB Output is correct
13 Correct 112 ms 10964 KB Output is correct
14 Correct 79 ms 8796 KB Output is correct
15 Correct 10 ms 10384 KB Output is correct
16 Correct 8 ms 11216 KB Output is correct
17 Correct 15 ms 11212 KB Output is correct
18 Correct 70 ms 11568 KB Output is correct
19 Correct 251 ms 12156 KB Output is correct
20 Correct 459 ms 11940 KB Output is correct
21 Correct 365 ms 11600 KB Output is correct
22 Correct 421 ms 13156 KB Output is correct
23 Correct 422 ms 13140 KB Output is correct
24 Correct 329 ms 13392 KB Output is correct
25 Correct 153 ms 16876 KB Output is correct
26 Correct 166 ms 16072 KB Output is correct
27 Correct 187 ms 16632 KB Output is correct
28 Correct 280 ms 13096 KB Output is correct
29 Correct 169 ms 17544 KB Output is correct
30 Correct 135 ms 14424 KB Output is correct
31 Correct 95 ms 23268 KB Output is correct
32 Correct 441 ms 13140 KB Output is correct
33 Correct 165 ms 17524 KB Output is correct
34 Correct 221 ms 17204 KB Output is correct
35 Correct 200 ms 18388 KB Output is correct