#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> deg(N);
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
deg[--x]++;
deg[--y]++;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> v(N), par(N, -1), dep(N), sz(N);
int cnt = 0, leaves = 0;
auto init = [&](auto init, int x) -> void {
if (deg[x] == 1) {
v[x] = 1;
leaves++;
}
if (par[x] != -1) {
adj[x].erase(find(adj[x].begin(), adj[x].end(), par[x]));
}
sz[x] = 1;
for (int &y : adj[x]) {
par[y] = x;
dep[y] = dep[x] + 1;
init(init, y);
sz[x] += sz[y];
v[x] ^= v[y];
if (sz[y] > sz[adj[x][0]]) {
swap(y, adj[x][0]);
}
}
cnt += v[x];
};
init(init, 0);
int K = 2 << __lg(N - 1);
vector<int> top(N), in(N), lazy(2 * K), s(2 * K), t(2 * K);
int timer = 0;
auto dfs = [&](auto dfs, int x) -> void {
in[x] = timer++;
s[in[x] + K] = v[x];
t[in[x] + K] = 1;
for (int y : adj[x]) {
top[y] = y == adj[x][0] ? top[x] : y;
dfs(dfs, y);
}
};
dfs(dfs, 0);
auto apply = [&](int p, int z) {
if (z) {
s[p] = t[p] - s[p];
lazy[p] ^= 1;
}
};
auto pull = [&](int p) {
s[p] = s[p * 2] + s[p * 2 + 1];
};
auto push = [&](int p) {
apply(p * 2, lazy[p]);
apply(p * 2 + 1, lazy[p]);
lazy[p] = 0;
};
auto update = [&](auto f, int p, int l, int r, int L, int R) -> void {
if (R <= l || r <= L) {
return;
}
if (L <= l && r <= R) {
apply(p, 1);
return;
}
int m = (l + r) / 2;
push(p);
f(f, p * 2, l, m, L, R);
f(f, p * 2 + 1, m, r, L, R);
pull(p);
};
for (int i = K - 1; i; i--) {
pull(i);
t[i] = t[i * 2] + t[i * 2 + 1];
}
while (Q--) {
int D;
cin >> D;
vector<int> a(D);
for (int &p : a) {
cin >> p;
p--;
if (deg[p]++ != 1) {
leaves++;
int x = p;
while (x != -1) {
update(update, 1, 0, K, in[top[x]], in[x] + 1);
x = par[top[x]];
}
}
}
if (leaves % 2 == 1) {
cout << -1 << "\n";
} else {
cout << 2 * (N - 1) - s[1] + D << "\n";
}
for (int p : a) {
if (--deg[p] != 1) {
leaves--;
int x = p;
while (x != -1) {
update(update, 1, 0, K, in[top[x]], in[x] + 1);
x = par[top[x]];
}
}
}
}
return 6/22;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
90 ms |
2972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
1108 KB |
Output is correct |
2 |
Correct |
45 ms |
1108 KB |
Output is correct |
3 |
Correct |
32 ms |
12224 KB |
Output is correct |
4 |
Correct |
65 ms |
10032 KB |
Output is correct |
5 |
Correct |
64 ms |
12600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
2004 KB |
Output is correct |
2 |
Correct |
37 ms |
2012 KB |
Output is correct |
3 |
Correct |
51 ms |
25664 KB |
Output is correct |
4 |
Correct |
97 ms |
24100 KB |
Output is correct |
5 |
Correct |
37 ms |
23740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
4200 KB |
Output is correct |
2 |
Correct |
41 ms |
3028 KB |
Output is correct |
3 |
Correct |
7 ms |
2892 KB |
Output is correct |
4 |
Correct |
8 ms |
3832 KB |
Output is correct |
5 |
Correct |
9 ms |
4308 KB |
Output is correct |
6 |
Correct |
45 ms |
3828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
7756 KB |
Output is correct |
2 |
Correct |
203 ms |
7452 KB |
Output is correct |
3 |
Correct |
150 ms |
3960 KB |
Output is correct |
4 |
Correct |
182 ms |
7372 KB |
Output is correct |
5 |
Correct |
182 ms |
7392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
12408 KB |
Output is correct |
2 |
Correct |
97 ms |
17404 KB |
Output is correct |
3 |
Correct |
138 ms |
16164 KB |
Output is correct |
4 |
Correct |
116 ms |
18312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
90 ms |
2972 KB |
Output is correct |
3 |
Correct |
45 ms |
1108 KB |
Output is correct |
4 |
Correct |
45 ms |
1108 KB |
Output is correct |
5 |
Correct |
32 ms |
12224 KB |
Output is correct |
6 |
Correct |
65 ms |
10032 KB |
Output is correct |
7 |
Correct |
64 ms |
12600 KB |
Output is correct |
8 |
Correct |
37 ms |
2004 KB |
Output is correct |
9 |
Correct |
37 ms |
2012 KB |
Output is correct |
10 |
Correct |
51 ms |
25664 KB |
Output is correct |
11 |
Correct |
97 ms |
24100 KB |
Output is correct |
12 |
Correct |
37 ms |
23740 KB |
Output is correct |
13 |
Correct |
64 ms |
4200 KB |
Output is correct |
14 |
Correct |
41 ms |
3028 KB |
Output is correct |
15 |
Correct |
7 ms |
2892 KB |
Output is correct |
16 |
Correct |
8 ms |
3832 KB |
Output is correct |
17 |
Correct |
9 ms |
4308 KB |
Output is correct |
18 |
Correct |
45 ms |
3828 KB |
Output is correct |
19 |
Correct |
113 ms |
7756 KB |
Output is correct |
20 |
Correct |
203 ms |
7452 KB |
Output is correct |
21 |
Correct |
150 ms |
3960 KB |
Output is correct |
22 |
Correct |
182 ms |
7372 KB |
Output is correct |
23 |
Correct |
182 ms |
7392 KB |
Output is correct |
24 |
Correct |
172 ms |
12408 KB |
Output is correct |
25 |
Correct |
97 ms |
17404 KB |
Output is correct |
26 |
Correct |
138 ms |
16164 KB |
Output is correct |
27 |
Correct |
116 ms |
18312 KB |
Output is correct |
28 |
Correct |
123 ms |
7148 KB |
Output is correct |
29 |
Correct |
116 ms |
16672 KB |
Output is correct |
30 |
Correct |
57 ms |
12632 KB |
Output is correct |
31 |
Correct |
96 ms |
24004 KB |
Output is correct |
32 |
Correct |
199 ms |
7580 KB |
Output is correct |
33 |
Correct |
125 ms |
14736 KB |
Output is correct |
34 |
Correct |
119 ms |
17612 KB |
Output is correct |
35 |
Correct |
132 ms |
17496 KB |
Output is correct |