답안 #638959

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
638959 2022-09-08T06:05:22 Z jiahng Spring cleaning (CEOI20_cleaning) C++14
100 / 100
687 ms 35664 KB
#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);
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7376 KB Output is correct
2 Correct 241 ms 12584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 9632 KB Output is correct
2 Correct 16 ms 9548 KB Output is correct
3 Correct 121 ms 30128 KB Output is correct
4 Correct 215 ms 25332 KB Output is correct
5 Correct 305 ms 32100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 10324 KB Output is correct
2 Correct 16 ms 10324 KB Output is correct
3 Correct 56 ms 35664 KB Output is correct
4 Correct 154 ms 34668 KB Output is correct
5 Correct 51 ms 32972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 13116 KB Output is correct
2 Correct 94 ms 12140 KB Output is correct
3 Correct 24 ms 11968 KB Output is correct
4 Correct 17 ms 12244 KB Output is correct
5 Correct 23 ms 12624 KB Output is correct
6 Correct 101 ms 12580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 261 ms 23560 KB Output is correct
2 Correct 622 ms 23408 KB Output is correct
3 Correct 416 ms 15828 KB Output is correct
4 Correct 651 ms 23356 KB Output is correct
5 Correct 687 ms 23480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 413 ms 31464 KB Output is correct
2 Correct 234 ms 34188 KB Output is correct
3 Correct 203 ms 32872 KB Output is correct
4 Correct 177 ms 33804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7376 KB Output is correct
2 Correct 241 ms 12584 KB Output is correct
3 Correct 46 ms 9632 KB Output is correct
4 Correct 16 ms 9548 KB Output is correct
5 Correct 121 ms 30128 KB Output is correct
6 Correct 215 ms 25332 KB Output is correct
7 Correct 305 ms 32100 KB Output is correct
8 Correct 52 ms 10324 KB Output is correct
9 Correct 16 ms 10324 KB Output is correct
10 Correct 56 ms 35664 KB Output is correct
11 Correct 154 ms 34668 KB Output is correct
12 Correct 51 ms 32972 KB Output is correct
13 Correct 141 ms 13116 KB Output is correct
14 Correct 94 ms 12140 KB Output is correct
15 Correct 24 ms 11968 KB Output is correct
16 Correct 17 ms 12244 KB Output is correct
17 Correct 23 ms 12624 KB Output is correct
18 Correct 101 ms 12580 KB Output is correct
19 Correct 261 ms 23560 KB Output is correct
20 Correct 622 ms 23408 KB Output is correct
21 Correct 416 ms 15828 KB Output is correct
22 Correct 651 ms 23356 KB Output is correct
23 Correct 687 ms 23480 KB Output is correct
24 Correct 413 ms 31464 KB Output is correct
25 Correct 234 ms 34188 KB Output is correct
26 Correct 203 ms 32872 KB Output is correct
27 Correct 177 ms 33804 KB Output is correct
28 Correct 444 ms 21708 KB Output is correct
29 Correct 281 ms 33512 KB Output is correct
30 Correct 189 ms 32140 KB Output is correct
31 Correct 167 ms 34700 KB Output is correct
32 Correct 638 ms 23440 KB Output is correct
33 Correct 347 ms 28200 KB Output is correct
34 Correct 383 ms 33300 KB Output is correct
35 Correct 375 ms 33100 KB Output is correct