Submission #574636

# Submission time Handle Problem Language Result Execution time Memory
574636 2022-06-09T05:48:29 Z piOOE Spring cleaning (CEOI20_cleaning) C++17
53 / 100
1000 ms 20560 KB
#include <bits/stdc++.h>

using namespace std;

#define sz(x) ((int)size(x))
#define all(x) begin(x), end(x)
#define trace(x) cout << #x << ": " << (x) << endl;

typedef long long ll;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }

const int N = 200000, infI = 1e9 + 7;
const ll infL = 3e18;

vector<int> g[N];
ll dp[N];//LL???

int cnt[N], parent[N], depth[N];

void pull(int v) {
    dp[v] = 0;
    if (sz(g[v]) <= 1) {
        //v is a leaf
        cnt[v] = 1;
    } else {
        cnt[v] = 0;
        for (int to: g[v]) {
            if (to != parent[v]) {
                cnt[v] += cnt[to];
                dp[v] += dp[to];
            }
        }
    }
    dp[v] += 2 - (cnt[v] & 1);
}

void dfs(int v, int p) {
    if (p != -1) {
        depth[v] = depth[p] + 1;
    }
    dp[v] = 0;
    parent[v] = p;
    if (sz(g[v]) <= 1) {
        //v is a leaf
        cnt[v] = 1;
    } else {
        cnt[v] = 0;
        for (int to: g[v]) {
            if (to != parent[v]) {
                dfs(to, v);
                cnt[v] += cnt[to];
                dp[v] += dp[to];
            }
        }
    }
    dp[v] += 2 - (cnt[v] & 1);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    int root = -1;
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int mxx = 0;
    for (int i = 0; i < n; ++i) {
        mxx = max(mxx, sz(g[i]));
    }
    for (int i = 0; i < n; ++i) {
        if (sz(g[i]) > 1) {
            root = i;
            break;
        }
    }
    assert(root != -1);
    dfs(root, -1);
    if (mxx <= 3 && *max_element(all(depth)) <= 18) {
        while (q--) {
            int D;
            cin >> D;
            vector<int> a(D);
            for (int i = 0; i < D; ++i) {
                cin >> a[i];
                --a[i];
            }
            sort(all(a));
            vector<int> b;
            int extra = 0;
            for (int i = 0; i < D;) {
                int j = i;
                while (j < D && a[i] == a[j])
                    ++j;
                if ((j - i) & 1) {
                    b.push_back(a[i]);
                    extra += j - i - 1;
                } else {
                    b.push_back(a[i]);
                    b.push_back(a[i]);
                    extra += j - i - 2;
                }
                i = j;
            }
            swap(a, b);
            D = sz(a);
            for (int i = 0; i < D; ++i) {
                g[a[i]].push_back(i + n);
            }
            for (int i = 0; i < D; ++i) {
                pull(i + n);
                if (i == D - 1 || a[i + 1] != a[i]) {
                    int x = a[i];
                    while (x != -1) {
                        pull(x);
                        x = parent[x];
                    }
                }
            }
            cout << (cnt[root] % 2 == 1 ? -1 : dp[root] + extra - 2) << '\n';
            for (int i = 0; i < D; ++i) {
                g[a[i]].pop_back();
                if (i == D - 1 || a[i + 1] != a[i]) {
                    int x = a[i];
                    while (x != -1) {
                        pull(x);
                        x = parent[x];
                    }
                }
            }
        }
    } else {
        while (q--) {
            int D;
            cin >> D;
            vector<int> a(D);
            for (int i = 0; i < D; ++i) {
                cin >> a[i];
                --a[i];
            }
            for (int i = 0; i < D; ++i) {
                g[a[i]].push_back(i + n);
            }
            dfs(root, -1);
            cout << (cnt[root] % 2 == 1 ? -1 : dp[root] - 2) << '\n';
            for (int i = 0; i < D; ++i) {
                g[a[i]].pop_back();
            }
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Execution timed out 1089 ms 6764 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 7968 KB Output is correct
2 Correct 22 ms 7972 KB Output is correct
3 Correct 34 ms 11324 KB Output is correct
4 Correct 44 ms 12084 KB Output is correct
5 Correct 55 ms 13976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 8532 KB Output is correct
2 Correct 17 ms 8652 KB Output is correct
3 Correct 74 ms 18956 KB Output is correct
4 Correct 74 ms 20560 KB Output is correct
5 Correct 46 ms 17664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 223 ms 7412 KB Output is correct
2 Correct 163 ms 6608 KB Output is correct
3 Correct 200 ms 6184 KB Output is correct
4 Correct 263 ms 6876 KB Output is correct
5 Correct 230 ms 7016 KB Output is correct
6 Correct 247 ms 7108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 9816 KB Output is correct
2 Correct 108 ms 9932 KB Output is correct
3 Correct 85 ms 7788 KB Output is correct
4 Correct 113 ms 9892 KB Output is correct
5 Correct 127 ms 9920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1074 ms 11324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Execution timed out 1089 ms 6764 KB Time limit exceeded
3 Halted 0 ms 0 KB -