답안 #585879

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
585879 2022-06-29T13:25:11 Z MohamedFaresNebili Spring cleaning (CEOI20_cleaning) C++14
100 / 100
615 ms 17740 KB
#include <bits/stdc++.h>
/// #pragma GCC optimize ("Ofast")
/// #pragma GCC target ("avx2")
/// #pragma GCC optimize("unroll-loops")

        using namespace std;

        using ll = long long;
        using ii = pair<int, int>;
        using vi = vector<int>;

        #define ff first
        #define ss second
        #define pb push_back
        #define all(x) (x).begin(), (x).end()
        #define lb lower_bound

        const int MOD = 1000 * 1000 * 1000 + 7;

        int N, Q, R, L, T, timer;
        int s[100001], par[100001];
        int tin[100001], head[100001], heavy[100001];
        int st[400004], lazy[400004];
        int vis[100001];
        vector<int> adj[100001];

        void prop(int v, int l, int r) {
            if(l == r || !lazy[v]) return;
            int md = (l + r) / 2;
            int A = md - l + 1, B = r - md;
            st[v * 2] = A - st[v * 2];
            st[v * 2 + 1] = B - st[v * 2 + 1];
            lazy[v * 2] ^= 1, lazy[v * 2 + 1] ^= 1;
            lazy[v] = 0;
        }

        void update(int v, int l, int r, int lo, int hi) {
            prop(v, l, r);
            if(l > hi || r < lo) return;
            if(l >= lo && r <= hi) {
                lazy[v] ^= 1;
                st[v] = r - l + 1 - st[v];
                prop(v, l, r);
                return;
            }
            int md = (l + r) / 2;
            update(v * 2, l, md, lo, hi);
            update(v * 2 + 1, md + 1, r, lo, hi);
            st[v] = st[v * 2] + st[v * 2 + 1];
        }
        void dfs(int v, int p) {
            s[v] = 1; par[v] = p; heavy[v] = 0;
            for(auto u : adj[v]) {
                if(u == p) continue;
                dfs(u, v); s[v] += s[u];
                if(s[u] > s[heavy[v]])
                    heavy[v] = u;
            }
        }
        void HLD(int v, int p, int t) {
            tin[v] = timer++; head[v] = t;
            if(heavy[v] == 0) return;
            HLD(heavy[v], v, t);
            for(auto u : adj[v]) {
                if(u == p || u == heavy[v]) continue;
                HLD(u, v, u);
            }
        }

		int32_t main() {
            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
            cin >> N >> Q;
            for(int l = 0; l < N - 1; l++) {
                int U, V; cin >> U >> V;
                adj[U].pb(V), adj[V].pb(U);
            }
            R = 1;
            for(int l = 1; l <= N; l++)
                if(adj[l].size() > 1) {
                    R = l; break;
                }
            dfs(R, 0); HLD(R, 0, R);
            for(int l = 1; l <= N; l++) {
                if(adj[l].size() > 1) continue;
                ++L;
                for(int u = l; u; u = par[head[u]])
                    update(1, 0, N - 1, tin[head[u]], tin[u]);
            }
            while(Q--) {
                T++; int D; cin >> D;
                int A[D]; vector<int> rev;
                int C = D + L;
                for(int l = 0; l < D; l++) {
                    int V; cin >> V; A[l] = V;
                    if(adj[V].size() <= 1 && vis[V] != T) {
                        vis[V] = T, rev.pb(V); --C;
                    }
                }
                if(C & 1) {
                    cout << -1 << endl;
                    continue;
                }
                for(auto u : rev)
                    for(; u; u = par[head[u]])
                        update(1, 0, N - 1, tin[head[u]], tin[u]);
                for(int l = 0; l < D; l++)
                    for(int u = A[l]; u; u = par[head[u]])
                        update(1, 0, N - 1, tin[head[u]], tin[u]);

                int odd = st[1] + D;
                int even = N - 1 - st[1];
                cout << odd + 2 * even << endl;

                for(auto u : rev)
                    for(; u; u = par[head[u]])
                        update(1, 0, N - 1, tin[head[u]], tin[u]);
                for(int l = 0; l < D; l++)
                    for(int u = A[l]; u; u = par[head[u]])
                        update(1, 0, N - 1, tin[head[u]], tin[u]);
            }
            return 0;
		}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 292 ms 4504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 3284 KB Output is correct
2 Correct 9 ms 3412 KB Output is correct
3 Correct 75 ms 10496 KB Output is correct
4 Correct 164 ms 9540 KB Output is correct
5 Correct 203 ms 11324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 3540 KB Output is correct
2 Correct 8 ms 3412 KB Output is correct
3 Correct 49 ms 17548 KB Output is correct
4 Correct 142 ms 17700 KB Output is correct
5 Correct 57 ms 16524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 131 ms 5072 KB Output is correct
2 Correct 91 ms 4436 KB Output is correct
3 Correct 19 ms 4476 KB Output is correct
4 Correct 13 ms 5068 KB Output is correct
5 Correct 18 ms 5204 KB Output is correct
6 Correct 91 ms 5260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 437 ms 7644 KB Output is correct
2 Correct 615 ms 7504 KB Output is correct
3 Correct 464 ms 5216 KB Output is correct
4 Correct 572 ms 7504 KB Output is correct
5 Correct 615 ms 7604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 380 ms 10972 KB Output is correct
2 Correct 307 ms 14704 KB Output is correct
3 Correct 292 ms 14900 KB Output is correct
4 Correct 288 ms 14364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 292 ms 4504 KB Output is correct
3 Correct 51 ms 3284 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
5 Correct 75 ms 10496 KB Output is correct
6 Correct 164 ms 9540 KB Output is correct
7 Correct 203 ms 11324 KB Output is correct
8 Correct 46 ms 3540 KB Output is correct
9 Correct 8 ms 3412 KB Output is correct
10 Correct 49 ms 17548 KB Output is correct
11 Correct 142 ms 17700 KB Output is correct
12 Correct 57 ms 16524 KB Output is correct
13 Correct 131 ms 5072 KB Output is correct
14 Correct 91 ms 4436 KB Output is correct
15 Correct 19 ms 4476 KB Output is correct
16 Correct 13 ms 5068 KB Output is correct
17 Correct 18 ms 5204 KB Output is correct
18 Correct 91 ms 5260 KB Output is correct
19 Correct 437 ms 7644 KB Output is correct
20 Correct 615 ms 7504 KB Output is correct
21 Correct 464 ms 5216 KB Output is correct
22 Correct 572 ms 7504 KB Output is correct
23 Correct 615 ms 7604 KB Output is correct
24 Correct 380 ms 10972 KB Output is correct
25 Correct 307 ms 14704 KB Output is correct
26 Correct 292 ms 14900 KB Output is correct
27 Correct 288 ms 14364 KB Output is correct
28 Correct 362 ms 8360 KB Output is correct
29 Correct 275 ms 14784 KB Output is correct
30 Correct 72 ms 12368 KB Output is correct
31 Correct 103 ms 17740 KB Output is correct
32 Correct 577 ms 8644 KB Output is correct
33 Correct 212 ms 12984 KB Output is correct
34 Correct 243 ms 14832 KB Output is correct
35 Correct 281 ms 14668 KB Output is correct