Submission #545525

# Submission time Handle Problem Language Result Execution time Memory
545525 2022-04-04T18:23:50 Z leaked Spring cleaning (CEOI20_cleaning) C++14
100 / 100
572 ms 21624 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const ll inf=1e18;
const int N=1e5+1;
vec<int> g[N];

int dp[N];
int t[4*N][2];
int p[4*N];
int par[N],in[N],out[N],tt=0,ht[N];
int up[N];

int have=0;
void build(int v,int tl,int tr){
    if(tl==tr){
        t[v][0]=1;
    }
    else{
        int tm=(tl+tr)>>1;
        build(2*v,tl,tm);build(2*v+1,tm+1,tr);
        t[v][0]=tr-tl+1;
    }
}
void push(int v,int tl,int tr){
    if(!p[v])
        return;
    for(auto &u : {v<<1,v<<1|1})
        swap(t[u][0],t[u][1]),p[u]^=1;
    p[v]=0;
}
void _xor(int l,int r,int v,int tl,int tr){
    if(tl>=l&&tr<=r){
        swap(t[v][0],t[v][1]);
        p[v]^=1;
        return;
    }
    if(tl>r||tr<l)
        return;
    int tm=(tl+tr)>>1;push(v,tl,tr);
    _xor(l,r,2*v,tl,tm);_xor(l,r,2*v+1,tm+1,tr);
    t[v][0]=t[v<<1][0]+t[v<<1|1][0];
    t[v][1]=t[v<<1][1]+t[v<<1|1][1];
}
void dfs1(int v,int p){
    dp[v]=1;
    for(auto &z : g[v]){
        auto it=find(all(g[z]),v);
        g[z].erase(it,it+1);
        par[z]=v;
        dfs1(z,v);
        dp[v]+=dp[z];
        if(dp[z]>dp[g[v][0]])
            swap(z,g[v][0]);
    }
}
void dfs(int v,int who){
    if(who==-1) who=v;
    up[v]=who;
    in[v]=tt++;
    if(!sz(g[v])) ++have;
    for(auto &z : g[v]){
        ht[z]=ht[v]+1;
        dfs(z,who);
        who=-1;
    }
//    cout<<"V "<<v<<' '<<in[v]<<' '<<up[v]<<endl;
    out[v]=tt-1;
}
void addway(int v){
    while(v!=-1){
        assert((ht[v]-ht[up[v]])==(in[v]-in[up[v]]));
//        cout<<"ADD "<<in[up[v]]<<' '<<in[v]<<endl;
        _xor(in[up[v]],in[v],1,0,tt-1);
        v=par[up[v]];
    }
}
signed main(){
    fast_resp;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<n;i++){
        int v,u;
        cin>>v>>u;--v;--u;
        g[v].pb(u);g[u].pb(v);
    }

    int root=-1;
    for(int i=0;i<n;i++) root=(sz(g[i])>1?i:root);
    par[root]=-1;

    dfs1(root,root);
    dfs(root,-1);

    build(1,0,tt-1);
    for(int i=0;i<n;i++){
        if(sz(g[i])==0)
            addway(i);
    }
    while(m--){
        int d;
        cin>>d;
        int ans=0;
        vec<int> e;
        int nh=have;
        for(int i=0;i<d;i++){
            int x;
            cin>>x;--x;
            e.pb(x);
            addway(x);
            if(!sz(g[x])) addway(x),--nh;
            g[x].pb(i);
            ++nh;
        }
        ans=n+d-1;ans+=t[1][0];
        cout<<(nh%2?-1:ans-1)<<'\n';
        for(auto &x : e){
            addway(x);
            g[x].pop_back();
            if(!sz(g[x])) addway(x);
        }
    }
    return 0;
}
/*
7 1
1 2
2 4
4 5
5 6
5 7
3 4
2 2 4
1 1

*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 253 ms 5356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 4196 KB Output is correct
2 Correct 60 ms 4216 KB Output is correct
3 Correct 78 ms 12168 KB Output is correct
4 Correct 185 ms 11408 KB Output is correct
5 Correct 224 ms 13368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 4904 KB Output is correct
2 Correct 41 ms 4916 KB Output is correct
3 Correct 51 ms 21624 KB Output is correct
4 Correct 125 ms 21260 KB Output is correct
5 Correct 46 ms 19644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 5780 KB Output is correct
2 Correct 103 ms 5024 KB Output is correct
3 Correct 20 ms 4692 KB Output is correct
4 Correct 15 ms 5424 KB Output is correct
5 Correct 19 ms 5396 KB Output is correct
6 Correct 79 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 9284 KB Output is correct
2 Correct 564 ms 9184 KB Output is correct
3 Correct 447 ms 6128 KB Output is correct
4 Correct 564 ms 9196 KB Output is correct
5 Correct 572 ms 9164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 495 ms 13736 KB Output is correct
2 Correct 229 ms 16972 KB Output is correct
3 Correct 239 ms 16240 KB Output is correct
4 Correct 228 ms 16972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 253 ms 5356 KB Output is correct
3 Correct 56 ms 4196 KB Output is correct
4 Correct 60 ms 4216 KB Output is correct
5 Correct 78 ms 12168 KB Output is correct
6 Correct 185 ms 11408 KB Output is correct
7 Correct 224 ms 13368 KB Output is correct
8 Correct 45 ms 4904 KB Output is correct
9 Correct 41 ms 4916 KB Output is correct
10 Correct 51 ms 21624 KB Output is correct
11 Correct 125 ms 21260 KB Output is correct
12 Correct 46 ms 19644 KB Output is correct
13 Correct 122 ms 5780 KB Output is correct
14 Correct 103 ms 5024 KB Output is correct
15 Correct 20 ms 4692 KB Output is correct
16 Correct 15 ms 5424 KB Output is correct
17 Correct 19 ms 5396 KB Output is correct
18 Correct 79 ms 5752 KB Output is correct
19 Correct 360 ms 9284 KB Output is correct
20 Correct 564 ms 9184 KB Output is correct
21 Correct 447 ms 6128 KB Output is correct
22 Correct 564 ms 9196 KB Output is correct
23 Correct 572 ms 9164 KB Output is correct
24 Correct 495 ms 13736 KB Output is correct
25 Correct 229 ms 16972 KB Output is correct
26 Correct 239 ms 16240 KB Output is correct
27 Correct 228 ms 16972 KB Output is correct
28 Correct 377 ms 8888 KB Output is correct
29 Correct 220 ms 15868 KB Output is correct
30 Correct 214 ms 13380 KB Output is correct
31 Correct 127 ms 21284 KB Output is correct
32 Correct 564 ms 9204 KB Output is correct
33 Correct 202 ms 15436 KB Output is correct
34 Correct 260 ms 15564 KB Output is correct
35 Correct 245 ms 16716 KB Output is correct