답안 #660711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
660711 2022-11-22T20:43:28 Z Lobo Spring cleaning (CEOI20_cleaning) C++17
28 / 100
1000 ms 10912 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 1e5+10;

int n, q, gr[maxn], p[maxn], sbl[maxn];
vector<int> g[maxn];
int cntl, ans;
int tr[4*maxn], lz[4*maxn], tin[maxn], tout[maxn], timer;

void build(int no, int l, int r) {
    lz[no] = 0;
    if(l == r) {
        tr[no] = 0;
        return;
    }
    int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
    build(lc,l,mid);
    build(rc,mid+1,r);
    tr[no] = tr[lc]+tr[rc];
}

void flush(int no, int l, int r) {
    if(!lz[no]) return;

    tr[no] = (r-l+1)-tr[no];
    if(l != r) {
        int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
        lz[lc]^= 1;
        lz[rc]^= 1;
    }
    lz[no] = 0;
}
void upd(int no, int l, int r, int L, int R) {
    flush(no,l,r);
    if(l > R || r < L) return;
    if(l >= L && r <= R) {
        lz[no] = 1;
        flush(no,l,r);
        return;
    }
    int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
    upd(lc,l,mid,L,R);
    upd(rc,mid+1,r,L,R);
    tr[no] = tr[lc]+tr[rc];
}

int qry(int no, int l, int r, int L, int R) {
    flush(no,l,r);
    if(l > R || r < L) return 0;
    if(l >= L && r <= R) {
        return tr[no];
    }
    int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
    return qry(lc,l,mid,L,R)+qry(rc,mid+1,r,L,R);
}

void dfs(int u, int ant) {
    p[u] = ant;
    tin[u] = ++timer;
    if(gr[u] == 1) {
        cntl++;
        sbl[u] = 1;
    }
    else sbl[u] = 0;
    for(auto v : g[u]) if(v != ant) {
        dfs(v,u);
        sbl[u]^= sbl[v];
    }
    tout[u] = timer;
}

void change(int v) {
    while(v != -1) {
        if(sbl[v] == 1) ans--;
        sbl[v]^= 1;
        if(sbl[v] == 1) ans++;
        upd(1,1,n,tin[v],tin[v]);
        v = p[v];
    }
}

void solve() {
    cin >> n >> q;
    for(int i = 1; i <= n-1; i++) {
        int u,v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
        gr[u]++;
        gr[v]++;
    }

    int rt;
    for(int i = 1; i <= n; i++) {
        if(gr[i] != 1) {
            rt = i;
            break;
        }
    }
    dfs(rt,-1);

    build(1,1,n);
    for(int i = 1; i <= n; i++) {
        if(sbl[i] == 1) ans++;
        if(sbl[i] == 1) upd(1,1,n,tin[i],tin[i]);
    }

    while(q--) {
        int D; cin >> D;
        vector<int> as;
        for(int i = 0; i < D; i++) {
            int v; cin >> v;
            as.pb(v);

            if(gr[v] == 1) {
                cntl--;
                change(v);
            }
            gr[v]++;
            cntl++;
            change(v);
        }

        if(cntl%2 == 1) {
            cout << -1 << endl;
        }
        else {
            int q1 = ans+D;
            int q2 = n-1-ans;
            cout << q1 + 2*q2 << endl;
        }

        for(int i = 0; i < D; i++) {
            int v = as[i];
            gr[v]--;
            if(gr[v] == 1) {
                cntl++;
                change(v);
            }
            cntl--;
            change(v);
        }

    }
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}

Compilation message

cleaning.cpp: In function 'void flush(int, int, int)':
cleaning.cpp:38:31: warning: unused variable 'mid' [-Wunused-variable]
   38 |         int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
      |                               ^~~
cleaning.cpp: In function 'void solve()':
cleaning.cpp:110:8: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
  110 |     dfs(rt,-1);
      |     ~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 793 ms 4408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 3532 KB Output is correct
2 Correct 71 ms 3532 KB Output is correct
3 Correct 64 ms 10220 KB Output is correct
4 Correct 151 ms 9348 KB Output is correct
5 Correct 180 ms 10912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1082 ms 3476 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1090 ms 4948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 555 ms 7284 KB Output is correct
2 Correct 973 ms 7048 KB Output is correct
3 Correct 792 ms 4924 KB Output is correct
4 Correct 954 ms 7176 KB Output is correct
5 Correct 914 ms 7088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 10260 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 793 ms 4408 KB Output is correct
3 Correct 61 ms 3532 KB Output is correct
4 Correct 71 ms 3532 KB Output is correct
5 Correct 64 ms 10220 KB Output is correct
6 Correct 151 ms 9348 KB Output is correct
7 Correct 180 ms 10912 KB Output is correct
8 Execution timed out 1082 ms 3476 KB Time limit exceeded
9 Halted 0 ms 0 KB -