#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5;
int n, q;
int dep[N], cnt[N], res[N], sz[N], vis[N];
bool valid[N];
vector<int> g[N];
set<array<int, 2>> st[N];
void upd(int u, int p, int i) {
int h = dep[u] - dep[p], c = cnt[u] - cnt[p];
res[i] += 2 * c - h;
}
void add(int u, int i, int x) {
auto it = st[u].lower_bound({i, 0});
if (it != st[u].end() && (*it)[0] == i) {
upd((*it)[1], u, i);
upd(x, u, i);
st[u].erase(it);
} else {
st[u].insert({i, x});
}
}
int calc(int u, int p) {
sz[u] = g[u].size() == 1;
int res = 0;
for (int v : g[u]) {
if (v ^ p) {
res += calc(v, u) + (sz[v] & 1 ? 1 : 2);
sz[u] += sz[v];
}
}
return res;
}
void dfs(int u, int p) {
dep[u] = dep[p] + 1;
cnt[u] = cnt[p] + (sz[u] & 1);
for (int v : g[u]) {
if (v ^ p) {
dfs(v, u);
if (st[u].size() < st[v].size()) {
st[u].swap(st[v]);
}
for (auto [x, y] : st[v]) {
add(u, x, y);
}
set<array<int, 2>>().swap(st[v]);
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int root = -1;
for (int i = 1; i <= n; ++i) {
if (g[i].size() > 1) {
root = i;
}
}
int ans = calc(root, root);
for (int i = 1; i <= q; ++i) {
int d; cin >> d;
res[i] = d;
int cnt = sz[root] + d;
while (d--) {
int u; cin >> u;
if (vis[u] != i && g[u].size() == 1) {
vis[u] = i;
--cnt;
continue;
}
add(u, i, u);
}
valid[i] = cnt % 2 == 0;
}
dfs(root, 0);
for (auto [i, u] : st[root]) {
upd(u, root, i);
}
for (int i = 1; i <= q; ++i) {
cout << (valid[i] ? ans + res[i] : -1) << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
43 ms |
11244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8024 KB |
Output is correct |
2 |
Correct |
10 ms |
8028 KB |
Output is correct |
3 |
Correct |
28 ms |
13268 KB |
Output is correct |
4 |
Correct |
27 ms |
12876 KB |
Output is correct |
5 |
Correct |
32 ms |
14548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9048 KB |
Output is correct |
2 |
Correct |
11 ms |
9052 KB |
Output is correct |
3 |
Correct |
42 ms |
28388 KB |
Output is correct |
4 |
Correct |
52 ms |
28756 KB |
Output is correct |
5 |
Correct |
35 ms |
26408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
11260 KB |
Output is correct |
2 |
Correct |
15 ms |
9308 KB |
Output is correct |
3 |
Correct |
8 ms |
8540 KB |
Output is correct |
4 |
Correct |
10 ms |
9468 KB |
Output is correct |
5 |
Correct |
11 ms |
9564 KB |
Output is correct |
6 |
Correct |
23 ms |
11612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
14368 KB |
Output is correct |
2 |
Correct |
56 ms |
13900 KB |
Output is correct |
3 |
Correct |
42 ms |
12124 KB |
Output is correct |
4 |
Correct |
57 ms |
13904 KB |
Output is correct |
5 |
Correct |
54 ms |
14096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
18112 KB |
Output is correct |
2 |
Correct |
74 ms |
23888 KB |
Output is correct |
3 |
Correct |
86 ms |
24652 KB |
Output is correct |
4 |
Correct |
84 ms |
25172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
43 ms |
11244 KB |
Output is correct |
3 |
Correct |
12 ms |
8024 KB |
Output is correct |
4 |
Correct |
10 ms |
8028 KB |
Output is correct |
5 |
Correct |
28 ms |
13268 KB |
Output is correct |
6 |
Correct |
27 ms |
12876 KB |
Output is correct |
7 |
Correct |
32 ms |
14548 KB |
Output is correct |
8 |
Correct |
11 ms |
9048 KB |
Output is correct |
9 |
Correct |
11 ms |
9052 KB |
Output is correct |
10 |
Correct |
42 ms |
28388 KB |
Output is correct |
11 |
Correct |
52 ms |
28756 KB |
Output is correct |
12 |
Correct |
35 ms |
26408 KB |
Output is correct |
13 |
Correct |
24 ms |
11260 KB |
Output is correct |
14 |
Correct |
15 ms |
9308 KB |
Output is correct |
15 |
Correct |
8 ms |
8540 KB |
Output is correct |
16 |
Correct |
10 ms |
9468 KB |
Output is correct |
17 |
Correct |
11 ms |
9564 KB |
Output is correct |
18 |
Correct |
23 ms |
11612 KB |
Output is correct |
19 |
Correct |
61 ms |
14368 KB |
Output is correct |
20 |
Correct |
56 ms |
13900 KB |
Output is correct |
21 |
Correct |
42 ms |
12124 KB |
Output is correct |
22 |
Correct |
57 ms |
13904 KB |
Output is correct |
23 |
Correct |
54 ms |
14096 KB |
Output is correct |
24 |
Correct |
98 ms |
18112 KB |
Output is correct |
25 |
Correct |
74 ms |
23888 KB |
Output is correct |
26 |
Correct |
86 ms |
24652 KB |
Output is correct |
27 |
Correct |
84 ms |
25172 KB |
Output is correct |
28 |
Correct |
48 ms |
13904 KB |
Output is correct |
29 |
Correct |
55 ms |
20236 KB |
Output is correct |
30 |
Correct |
32 ms |
14544 KB |
Output is correct |
31 |
Correct |
51 ms |
28708 KB |
Output is correct |
32 |
Correct |
55 ms |
13908 KB |
Output is correct |
33 |
Correct |
58 ms |
22356 KB |
Output is correct |
34 |
Correct |
70 ms |
21584 KB |
Output is correct |
35 |
Correct |
70 ms |
23376 KB |
Output is correct |