Submission #574648

# Submission time Handle Problem Language Result Execution time Memory
574648 2022-06-09T06:34:02 Z piOOE Spring cleaning (CEOI20_cleaning) C++17
17 / 100
111 ms 16216 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], cnt1[N], cnt2[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);
}

void gfs(int v) {
    if (parent[v] != -1) {
        cnt1[v] = cnt1[parent[v]];
        cnt2[v] = cnt2[parent[v]];
    }
    if (cnt[v] & 1) {
        ++cnt1[v];
    } else {
        ++cnt2[v];
    }
    for (int to: g[v]) {
        if (to != parent[v]) {
            gfs(to);
        }
    }
}

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);
    gfs(root);
    while (q--) {
        int D;
        cin >> D;
        vector<int> a(D);
        for (int i = 0; i < D; ++i) {
            cin >> a[i];
            --a[i];
        }
        if (D == 1) {
            if (sz(g[a[0]]) == 1) {
                cout << ((cnt[root] % 2 == 1 ? -1 : dp[root] - 1)) << '\n';
            } else {
                cout << (cnt[root] % 2 == 0 ? -1 : dp[root] - cnt2[a[0]] * 2 - cnt1[a[0]] * 1 + cnt2[a[0]] * 1 +
                                                   cnt1[a[0]] * 2 - 1) << '\n';
            }
        } else {
            exit(1);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 4948 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 9 ms 5724 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 6304 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 6996 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 44 ms 9316 KB Output is correct
2 Runtime error 39 ms 9024 KB Execution failed because the return code was nonzero
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 12076 KB Output is correct
2 Correct 88 ms 16116 KB Output is correct
3 Correct 97 ms 16216 KB Output is correct
4 Correct 82 ms 15572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 4948 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -