Submission #638959

#TimeUsernameProblemLanguageResultExecution timeMemory
638959jiahngSpring cleaning (CEOI20_cleaning)C++14
100 / 100
687 ms35664 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 300010
#define INF (ll)(1e18+10)
#define MOD 998244353
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;
int TC;

vi adj[maxn];
int sz[maxn], heavy[maxn], depth[maxn], head[maxn], st[maxn], par[maxn];
void dfs(int x,int p){
    sz[x] = 1;
    par[x] = p;
    heavy[x] = -1;
    aFOR(i,adj[x]) if (i != p){
        depth[i] = depth[x] + 1;
        dfs(i,x);
        sz[x] += sz[i];
        if (heavy[x] == -1 || sz[heavy[x]] < sz[i]) heavy[x] = i;
    }
}
int co = 1;
void hld(int x, int h){
    head[x] = h; st[x] = co++;
    if (heavy[x] != -1) hld(heavy[x], h);
    aFOR(i, adj[x]) if (i != heavy[x] && i != par[x]){
        hld(i, i);
    }
}

struct node{
    int s,e,m,val=0; // val is the number of odds
    bool lazy = 0;
    node *l, *r;

    node(int ss,int ee){
        s = ss; e = ee; m = (s + e) / 2;
        if (s != e){
            l = new node(s,m); r = new node(m+1,e);
        }
    }
    
    void prop(){
        if (s == e || !lazy) return;

        l->lazy = !l->lazy; r->lazy = !r->lazy;
        l->val = (l->e - l->s + 1) - l->val;
        r->val = (r->e - r->s + 1) - r->val;
        lazy = 0;
    }

    void flip(int a,int b){
        prop();
        if (a <= s && e <= b){
            val = (e - s + 1) - val;
            lazy = !lazy;
        }else if (a > e || s > b) return;
        else{
            l->flip(a, b); r->flip(a,b);
            val = l->val + r->val;
        }
    }
}*root;
        

void upd(int x){ // flip from x to root
    while (x != -1){
        root->flip(st[head[x]], st[x]);
        x = par[head[x]];
    }
}
int a,b;
int N,Q,d;
int deg[maxn];
int32_t main(){
    fast;
    cin >> N >> Q;
    FOR(i,1,N-1){
        cin >> a >> b; adj[a].pb(b); adj[b].pb(a);
    }

    dfs(1,-1);
    hld(1,1);
    root = new node(1,N);
    
    int num = 0;
    FOR(i,1,N) deg[i] = adj[i].size();
    FOR(i,1,N) if (adj[i].size() == 1){
        num++; upd(i);
    }

    FOR(i,1,Q){
        cin >> d;
        int nnum = num;
        vi v;
        FOR(j,1,d){
            cin >> a;
            if (deg[a] == 1){
                upd(a); nnum--; 
            }
            deg[a]++;
            v.pb(a);
        }
        if ((nnum+d) % 2 == 1){
            cout << -1 << '\n';
        }else{
            aFOR(i, v) upd(i);
            cout << (N - 1 - root->val) + (N-1) + d << '\n';
            aFOR(i, v) upd(i);
        }
        aFOR(i,v){
            deg[i]--;
            if (deg[i] == 1) upd(i);
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...