Submission #658322

# Submission time Handle Problem Language Result Execution time Memory
658322 2022-11-12T19:38:12 Z Kahou Spring cleaning (CEOI20_cleaning) C++14
100 / 100
247 ms 28228 KB
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int N = 1e5 + 50;
int n, q, h[N], par[20][N], st[N], ft[N], t, cnt[N], sub[N];
int oa, ans, lf[N];
int p[N];
bool mark[N];
vector<int> adj[N], adj2[N];
vector<int> vc;
 
void dfs(int u, int p) {
        st[u] = ++t;
        par[0][u] = p;
        h[u] = h[p]+1;
        for (int v:adj[u]) {
                if (v==p) continue;
                dfs(v, u);
                sub[u] += sub[v];
        }
        ft[u] = t;
}
void dfs2(int u, int p) {
        for (int v:adj[u]) {
                if (v == p) continue;
                cnt[v] = cnt[u] + 1-(sub[v]&1);
                oa += 1-(sub[v]&1);
                dfs2(v, u);
        }
}
int Par(int u, int x) {
        for (int k = 0; k < 20; k++) {
                if (x>>k&1) u = par[k][u];
        }
        return u;
}
int LCA(int u, int v) {
        if (h[u] < h[v]) swap(u, v);
        u = Par(u, h[u]-h[v]);
        for (int k = 19; k >= 0; k--) {
                if (par[k][u] != par[k][v]) {
                        u = par[k][u];
                        v = par[k][v];
                }
        }
        if (u == v) return u;
        return par[0][u];
}
 
int f(int u, int v) {
        return (h[v]-h[u])-2*(cnt[v]-cnt[u]);
}
 
void sfd(int u, int p) {
        for (int v:adj2[u]) {
                if (v == p) continue;
                sfd(v, u);
                lf[u] += lf[v];
                if (lf[v]&1) ans += f(u, v);
        }
}
 
bool cmp(int u, int v) {
        return st[u] < st[v];
}
 
void solve() {
        cin >> n >> q;
        for (int i = 1; i <= n-1; i++) {
                int u, v;
                cin >> u >> v;
                adj[u].push_back(v);
                adj[v].push_back(u);
        }
        int r = 0;
        for (int u = 1; u <= n; u++) {
                if(adj[u].size() > 1) {
                        if (!r) r = u;
                }
                else {
                        mark[u] = 1;
                        sub[u] = 1;
                }
        }
        dfs(r, 0);
        dfs2(r, 0);
        for (int k = 1; k < 20; k++) {
                for (int u = 1; u <= n; u++) {
                        par[k][u] = par[k-1][par[k-1][u]];
                }
        }
 
        for (int u = 1; u <= n; u++) {
                lf[u] = mark[u];
        }
        ans = oa;
        while (q--) {
                int d;
                cin >> d;
                for (int i = 1; i <= d; i++) {
                        int u;
                        cin >> u;
                        lf[u]++;
                        vc.push_back(u);
                }
                vc.push_back(r);
                sort(vc.begin(), vc.end(), cmp);
                vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
                int tmp = vc.size();
                for (int i = 1; i < tmp; i++) {
                        int u = vc[i];
                        int v = vc[i-1];
                        vc.push_back(LCA(u, v));
                }
                sort(vc.begin(), vc.end(), cmp);
                vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
                for (int i = 1; i < (int)vc.size(); i++) {
                        int u = vc[i];
                        p[u] = vc[i-1];
                        while (st[u] > ft[p[u]]) p[u] = p[p[u]];
                        adj2[u].push_back(p[u]);
                        adj2[p[u]].push_back(u);
                }
                sfd(vc[0], 0);
                cout << ((sub[r] + lf[vc[0]])&1 ? -1:n+d-1+ans) << endl;
                for (int v:vc) {
                        lf[v] = mark[v];
                        adj2[v].clear();
                }
                vc.clear();
                ans = oa;
        }
}
 
int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        solve();
        return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5144 KB Output is correct
2 Correct 71 ms 8580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 6228 KB Output is correct
2 Correct 17 ms 6100 KB Output is correct
3 Correct 39 ms 19428 KB Output is correct
4 Correct 74 ms 17216 KB Output is correct
5 Correct 100 ms 21700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 6744 KB Output is correct
2 Correct 18 ms 6744 KB Output is correct
3 Correct 54 ms 26420 KB Output is correct
4 Correct 87 ms 26672 KB Output is correct
5 Correct 42 ms 24284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 9180 KB Output is correct
2 Correct 33 ms 8388 KB Output is correct
3 Correct 11 ms 7900 KB Output is correct
4 Correct 11 ms 8532 KB Output is correct
5 Correct 12 ms 8660 KB Output is correct
6 Correct 45 ms 8976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 16492 KB Output is correct
2 Correct 146 ms 15784 KB Output is correct
3 Correct 93 ms 10904 KB Output is correct
4 Correct 161 ms 15852 KB Output is correct
5 Correct 159 ms 15884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 22540 KB Output is correct
2 Correct 118 ms 25192 KB Output is correct
3 Correct 114 ms 25300 KB Output is correct
4 Correct 123 ms 26720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5144 KB Output is correct
2 Correct 71 ms 8580 KB Output is correct
3 Correct 17 ms 6228 KB Output is correct
4 Correct 17 ms 6100 KB Output is correct
5 Correct 39 ms 19428 KB Output is correct
6 Correct 74 ms 17216 KB Output is correct
7 Correct 100 ms 21700 KB Output is correct
8 Correct 18 ms 6744 KB Output is correct
9 Correct 18 ms 6744 KB Output is correct
10 Correct 54 ms 26420 KB Output is correct
11 Correct 87 ms 26672 KB Output is correct
12 Correct 42 ms 24284 KB Output is correct
13 Correct 53 ms 9180 KB Output is correct
14 Correct 33 ms 8388 KB Output is correct
15 Correct 11 ms 7900 KB Output is correct
16 Correct 11 ms 8532 KB Output is correct
17 Correct 12 ms 8660 KB Output is correct
18 Correct 45 ms 8976 KB Output is correct
19 Correct 66 ms 16492 KB Output is correct
20 Correct 146 ms 15784 KB Output is correct
21 Correct 93 ms 10904 KB Output is correct
22 Correct 161 ms 15852 KB Output is correct
23 Correct 159 ms 15884 KB Output is correct
24 Correct 108 ms 22540 KB Output is correct
25 Correct 118 ms 25192 KB Output is correct
26 Correct 114 ms 25300 KB Output is correct
27 Correct 123 ms 26720 KB Output is correct
28 Correct 155 ms 16248 KB Output is correct
29 Correct 130 ms 25004 KB Output is correct
30 Correct 108 ms 22948 KB Output is correct
31 Correct 97 ms 28228 KB Output is correct
32 Correct 159 ms 17100 KB Output is correct
33 Correct 196 ms 22080 KB Output is correct
34 Correct 247 ms 25832 KB Output is correct
35 Correct 225 ms 25740 KB Output is correct