답안 #931705

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
931705 2024-02-22T09:46:47 Z sysia Spring cleaning (CEOI20_cleaning) C++17
100 / 100
195 ms 29640 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

struct Tree{
    vector<int>tab, lazy;
    int size = 1;

    Tree(int n){
        while (size < n) size*=2;
        tab.assign(2*size, 0);
        lazy.assign(2*size, 0);
    }

    //xor na przedziale suma na przedziale
    void push(int x, int lx, int rx){
        if (!lazy[x]) return;
        for (auto k: {2*x, 2*x+1}){
            tab[k] = (rx-lx+1)/2 - tab[k];
            lazy[k] ^= 1;
        }
        lazy[x] = 0;
    };

    void update(int x, int lx, int rx, int l, int r){
        if (lx > r || rx < l) return;
        if (lx >= l && rx <= r) {
            tab[x] = rx-lx+1-tab[x];
            lazy[x] ^= 1;
            return;
        }
        int m = (lx+rx)/2;
        push(x, lx, rx);
        update(2*x, lx, m, l, r);
        update(2*x+1, m+1, rx, l, r);
        tab[x] = tab[2*x] + tab[2*x+1];
    }

    int query(int x, int lx, int rx, int l, int r){
        if (lx > r || rx < l) return 0ll;
        if (lx >= l && rx <= r) return tab[x];
        push(x, lx, rx);
        int m = (lx+rx)/2;
        return query(2*x, lx, m, l, r) + query(2*x+1, m+1, rx, l, r);
    }

};

void solve(){
    int n, q; cin >> n >> q;
    vector<vector<int>>g(n+1);
    for (int i = 1; i<n; i++){
        int a, b; cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    vector<int>ord, sz(n+1, 1);
    function<void(int, int)>subsize = [&](int v, int pa){
        for (auto x: g[v]){
            if (x == pa) continue;
            subsize(x, v);
            sz[v] += sz[x];
        }
    };
    subsize(1, 1);
    debug(sz);
    vector<int>pos(n+1), head(n+1), par(n+1);
    function<void(int, int, int)>dfs = [&](int v, int pa, int h){
        pos[v] = (int)ord.size();
        par[v] = pa;
        head[v] = h;
        ord.emplace_back(v);
        int big = -1;
        for (auto x: g[v]){
            if (x == pa) continue;
            if (big == -1 || sz[x] > sz[big]) big = x;
        }
        if (big != -1) dfs(big, v, h);
        for (auto x: g[v]){
            if (x == pa || x == big) continue;
            dfs(x, v, x);
        }
    };
    dfs(1, 0, 1);
    debug(ord);
    debug(head);
    Tree t(n+2);
    vector<bool>leaf(n+1);
    int l = 0;
    auto add = [&](int v){
        while (v){
            int ll = max(1ll, pos[head[v]]);
            if (ll <= pos[v]) {
                t.update(1, 0, t.size-1, ll, pos[v]);
                debug(v, ll, pos[v]);
            }
            v = par[head[v]];
        }
    };
    for (int i = 1; i<=n; i++) {
        leaf[i] = ((int)g[i].size() == 1);
        if (leaf[i]) {
            l++;
            add(i);
        }
    }
    vector<int>ile(n+1);
    auto solve = [&](vector<int>vt){
        int d = (int)vt.size();
        int curr = l;
        auto fix = [&](){
            for (auto x: vt) leaf[x] = ((int)g[x].size() == 1);
            for (auto x: vt){
                if (ile[x]&1) add(x);
                ile[x] = 0;
            }
        };
        for (auto x: vt){
            if (leaf[x]) {
                leaf[x] = 0;
            } else {
                curr++;
                ile[x]++;
                add(x);
            }
        }
        if (curr&1) {
            cout << "-1\n";
            fix();
            return;
        }
        debug(t.query(1, 0, t.size-1, 0, n));
        cout << n-1+d+n-1-t.query(1, 0, t.size-1, 1, n-1) << "\n";
        fix();
    };
    while (q--){
        int d; cin >> d;
        vector<int>vt(d);
        for (auto &x: vt) cin >> x;
        solve(vt);
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 69 ms 3816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 2392 KB Output is correct
2 Correct 15 ms 2144 KB Output is correct
3 Correct 42 ms 15440 KB Output is correct
4 Correct 46 ms 13648 KB Output is correct
5 Correct 57 ms 16964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 3160 KB Output is correct
2 Correct 22 ms 3420 KB Output is correct
3 Correct 46 ms 29640 KB Output is correct
4 Correct 103 ms 29488 KB Output is correct
5 Correct 41 ms 27372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 5256 KB Output is correct
2 Correct 32 ms 4056 KB Output is correct
3 Correct 13 ms 3800 KB Output is correct
4 Correct 11 ms 5080 KB Output is correct
5 Correct 14 ms 5340 KB Output is correct
6 Correct 45 ms 4832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 12232 KB Output is correct
2 Correct 178 ms 13532 KB Output is correct
3 Correct 122 ms 7276 KB Output is correct
4 Correct 165 ms 13516 KB Output is correct
5 Correct 162 ms 13528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 195 ms 16092 KB Output is correct
2 Correct 121 ms 23492 KB Output is correct
3 Correct 145 ms 21324 KB Output is correct
4 Correct 147 ms 24016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 69 ms 3816 KB Output is correct
3 Correct 14 ms 2392 KB Output is correct
4 Correct 15 ms 2144 KB Output is correct
5 Correct 42 ms 15440 KB Output is correct
6 Correct 46 ms 13648 KB Output is correct
7 Correct 57 ms 16964 KB Output is correct
8 Correct 22 ms 3160 KB Output is correct
9 Correct 22 ms 3420 KB Output is correct
10 Correct 46 ms 29640 KB Output is correct
11 Correct 103 ms 29488 KB Output is correct
12 Correct 41 ms 27372 KB Output is correct
13 Correct 51 ms 5256 KB Output is correct
14 Correct 32 ms 4056 KB Output is correct
15 Correct 13 ms 3800 KB Output is correct
16 Correct 11 ms 5080 KB Output is correct
17 Correct 14 ms 5340 KB Output is correct
18 Correct 45 ms 4832 KB Output is correct
19 Correct 124 ms 12232 KB Output is correct
20 Correct 178 ms 13532 KB Output is correct
21 Correct 122 ms 7276 KB Output is correct
22 Correct 165 ms 13516 KB Output is correct
23 Correct 162 ms 13528 KB Output is correct
24 Correct 195 ms 16092 KB Output is correct
25 Correct 121 ms 23492 KB Output is correct
26 Correct 145 ms 21324 KB Output is correct
27 Correct 147 ms 24016 KB Output is correct
28 Correct 110 ms 10188 KB Output is correct
29 Correct 122 ms 22380 KB Output is correct
30 Correct 57 ms 18252 KB Output is correct
31 Correct 98 ms 29236 KB Output is correct
32 Correct 183 ms 13516 KB Output is correct
33 Correct 119 ms 18896 KB Output is correct
34 Correct 139 ms 22620 KB Output is correct
35 Correct 155 ms 22548 KB Output is correct