Submission #868684

# Submission time Handle Problem Language Result Execution time Memory
868684 2023-11-01T14:17:40 Z ToighetLPHM Spring cleaning (CEOI20_cleaning) C++17
100 / 100
725 ms 22032 KB
#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define FOD(i,a,b) for (int i=(a);i>=(b);i--)
#define bit(x,y) ((x)>>(y))&1
#define pb push_back
#define ll long long
#define ii pair < int,int >
#define f first
#define s second
#define M 1000000007
#define N 100005
using namespace std;
int n,q,pos[N],numchild[N],head[N],depth[N],dad[N],numleaf[N],leaf=0,Time=0;
char tv;
vector < int > adj[N];
struct seg {
    int cnt0,cnt1,lazy;
} T[4*N];
void dfs (int u,int par) {
    dad[u]=par;
    numchild[u]=1;
    int posmax=0,maxchild=0;
    FOR(i,0,adj[u].size()-1) {
        int v=adj[u][i];
        if (v!=par) {
            dfs(v,u);
            numchild[u]+=numchild[v];
            numleaf[u]+=numleaf[v];
            posmax = (numchild[v]>maxchild) ? i : posmax;
            maxchild = (numchild[v]>maxchild) ? numchild[v] : maxchild;
        }
    }
    swap(adj[u][0],adj[u][posmax]);
    if (numchild[u]==1) {
        ++leaf;
        numleaf[u]=1;
    }
}
int node[N];
void HLD (int u) {
    pos[u]=++Time;
    node[Time]=u;
    for (auto v : adj[u])
    if (dad[v]==u) {
        if (numchild[v]*2>=numchild[u]) {
            head[v]=head[u];
            depth[v]=depth[u];
        }
        else {
            head[v]=v;
            depth[v]=depth[u]+1;
        }
        HLD(v);
    }
}
void Build(int id,int l,int r) {
    if (l==r) {
        T[id].cnt0=(numleaf[node[l]]%2==0);
        T[id].cnt1=(numleaf[node[l]]%2==1);
        T[id].lazy=0;
        return;
    }
    int mid=(l+r)/2;
    Build(id*2,l,mid);
    Build(id*2+1,mid+1,r);
    T[id].cnt0=T[id*2].cnt0+T[id*2+1].cnt0;
    T[id].cnt1=T[id*2].cnt1+T[id*2+1].cnt1;
    T[id].lazy=0;
}
void down (int id) {
    if (T[id].lazy%2==0) return;
    swap(T[id*2].cnt0,T[id*2].cnt1);
    T[id*2].lazy=T[id*2].lazy+T[id].lazy;
    //
    swap(T[id*2+1].cnt0,T[id*2+1].cnt1);
    T[id*2+1].lazy=T[id*2+1].lazy+T[id].lazy;
    //
    T[id].lazy=0;
}
void update (int id,int l,int r,int c,int d,int x) {
    if (r<c || l>d) return;
    if (c<=l && r<=d) {
        swap(T[id].cnt0,T[id].cnt1);
        T[id].lazy=T[id].lazy+x;
        return;
    }
    down(id);
    int mid=(l+r)/2;
    update(id*2,l,mid,c,d,x);
    update(id*2+1,mid+1,r,c,d,x);
    T[id].cnt0=T[id*2].cnt0+T[id*2+1].cnt0;
    T[id].cnt1=T[id*2].cnt1+T[id*2+1].cnt1;
}
void incroad (int u,int v,int x) {
    if (depth[u]>depth[v]) swap(u,v);
    while (depth[v]>depth[u]) {
        update(1,1,n,pos[head[v]],pos[v],x);
        v=head[v];
        v=dad[v];
    }
    while (head[u]!=head[v]) {
        update(1,1,n,pos[head[u]],pos[u],x);
        u=head[u];
        u=dad[u];
        update(1,1,n,pos[head[v]],pos[v],x);
        v=head[v];
        v=dad[v];
    }
    if (pos[u]<pos[v]) update(1,1,n,pos[u],pos[v],x);
    else update(1,1,n,pos[v],pos[u],x);
}
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>q;
    FOR(i,1,n-1) {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    int root;
    FOR(i,1,n)
    if (adj[i].size()>1) root=i;
    //cout<<root<<'\n';
    dfs(root,root);
    head[root]=root;
    HLD(root);
    Build(1,1,n);
    //cout<<T[1].cnt0<<'\n';
    while (q--) {
        int d;
        cin>>d;
        vector < int > a;
        int e=n-1;
        FOR(i,1,d) {
            int x;
            cin>>x;
            a.pb(x);
            if (numchild[x]==1) {
                --leaf;
                incroad(x,root,1);
            }
            ++numchild[x];
            ++leaf;
            incroad(x,root,1);
            ++e;
        }
        //cout<<e<<" "<<leaf<<'\n';
        if (leaf&1) cout<<-1<<'\n';
        else {
            e+=T[1].cnt0-1;
            cout<<e<<'\n';
        }
        FOR(i,0,d-1) {
            --numchild[a[i]];
            if (numchild[a[i]]==1) {
                ++leaf;
                incroad(a[i],root,-1);
            }
            --leaf;
            incroad(a[i],root,-1);
        }
    }
    return 0;
}

Compilation message

cleaning.cpp: In function 'void dfs(int, int)':
cleaning.cpp:2:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define FOR(i,a,b) for (int i=(a);i<=(b);i++)
      |                                    ^
cleaning.cpp:23:5: note: in expansion of macro 'FOR'
   23 |     FOR(i,0,adj[u].size()-1) {
      |     ^~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:147:20: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
  147 |             incroad(x,root,1);
      |             ~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 259 ms 7596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 7392 KB Output is correct
2 Correct 51 ms 7644 KB Output is correct
3 Correct 27 ms 14416 KB Output is correct
4 Correct 124 ms 14024 KB Output is correct
5 Correct 146 ms 15564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 7900 KB Output is correct
2 Correct 37 ms 7900 KB Output is correct
3 Correct 32 ms 21336 KB Output is correct
4 Correct 85 ms 20440 KB Output is correct
5 Correct 44 ms 19792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 8024 KB Output is correct
2 Correct 97 ms 7980 KB Output is correct
3 Correct 8 ms 7768 KB Output is correct
4 Correct 7 ms 8320 KB Output is correct
5 Correct 10 ms 8136 KB Output is correct
6 Correct 72 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 454 ms 10324 KB Output is correct
2 Correct 712 ms 11092 KB Output is correct
3 Correct 616 ms 9312 KB Output is correct
4 Correct 725 ms 11016 KB Output is correct
5 Correct 712 ms 11176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 398 ms 13908 KB Output is correct
2 Correct 160 ms 16768 KB Output is correct
3 Correct 176 ms 18004 KB Output is correct
4 Correct 168 ms 18512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 259 ms 7596 KB Output is correct
3 Correct 50 ms 7392 KB Output is correct
4 Correct 51 ms 7644 KB Output is correct
5 Correct 27 ms 14416 KB Output is correct
6 Correct 124 ms 14024 KB Output is correct
7 Correct 146 ms 15564 KB Output is correct
8 Correct 37 ms 7900 KB Output is correct
9 Correct 37 ms 7900 KB Output is correct
10 Correct 32 ms 21336 KB Output is correct
11 Correct 85 ms 20440 KB Output is correct
12 Correct 44 ms 19792 KB Output is correct
13 Correct 112 ms 8024 KB Output is correct
14 Correct 97 ms 7980 KB Output is correct
15 Correct 8 ms 7768 KB Output is correct
16 Correct 7 ms 8320 KB Output is correct
17 Correct 10 ms 8136 KB Output is correct
18 Correct 72 ms 8540 KB Output is correct
19 Correct 454 ms 10324 KB Output is correct
20 Correct 712 ms 11092 KB Output is correct
21 Correct 616 ms 9312 KB Output is correct
22 Correct 725 ms 11016 KB Output is correct
23 Correct 712 ms 11176 KB Output is correct
24 Correct 398 ms 13908 KB Output is correct
25 Correct 160 ms 16768 KB Output is correct
26 Correct 176 ms 18004 KB Output is correct
27 Correct 168 ms 18512 KB Output is correct
28 Correct 346 ms 10828 KB Output is correct
29 Correct 157 ms 17488 KB Output is correct
30 Correct 145 ms 15636 KB Output is correct
31 Correct 91 ms 22032 KB Output is correct
32 Correct 721 ms 11092 KB Output is correct
33 Correct 156 ms 16896 KB Output is correct
34 Correct 196 ms 17320 KB Output is correct
35 Correct 190 ms 18516 KB Output is correct