#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct SegmentTree {
int n;
vector<int> t, lz;
SegmentTree(int n) : n(n), t(4 * n), lz(4 * n) {}
void push(int p, int len) {
if (!lz[p])
return;
if (len > 1) {
lz[2 * p] ^= lz[p];
lz[2 * p + 1] ^= lz[p];
}
t[p] = len - t[p];
lz[p] = 0;
}
void update(int p, int l, int r, int x, int y) {
push(p, r - l);
if (r <= x || y <= l)
return;
if (x <= l && r <= y) {
lz[p] = 1;
push(p, r - l);
return;
}
int m = (l + r) / 2;
update(2 * p, l, m, x, y);
update(2 * p + 1, m, r, x, y);
t[p] = t[2 * p] + t[2 * p + 1];
}
void update(int l, int r) {
update(1, 0, n, l, r);
}
int get(int p, int l, int r, int x, int y) {
push(p, r - l);
if (r <= x || y <= l)
return 0;
if (x <= l && r <= y)
return t[p];
int m = (l + r) / 2;
return get(2 * p, l, m, x, y) + get(2 * p + 1, m, r, x, y);
}
int get(int l, int r) {
return get(1, 0, n, l, r);
}
};
/*
7 4
1 2
2 4
4 5
5 6
5 7
3 4
1 4
2 2 4
5 6 6 5 4 2
1 1
4 1
1 2
1 3
1 4
2 2 2
*/
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<vector<int>> e(n);
vector<int> deg(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
deg[u]++;
deg[v]++;
}
vector<int> c(n), in(n), out(n), par(n);
int timeStamp = 0;
int cnt = 0;
function<void(int, int)> dfs = [&](int u, int p) {
in[u] = timeStamp++;
c[u] = deg[u] <= 1;
cnt += c[u];
if (p != -1) {
e[u].erase(find(e[u].begin(), e[u].end(), p));
}
for (int &v : e[u]) {
dfs(v, u);
c[u] ^= c[v];
if (out[v] - in[v] > out[e[u][0]] - in[e[u][0]]) {
swap(e[u][0], v);
}
}
out[u] = timeStamp;
};
dfs(0, -1);
vector<int> nxt(n, 0);
timeStamp = 0;
dfs = [&](int u, int p) {
in[u] = timeStamp++;
par[u] = p;
for (int v : e[u]) {
nxt[v] = (v == e[u][0] ? nxt[u] : v);
dfs(v, u);
}
out[u] = timeStamp;
};
dfs(0, -1);
SegmentTree t(n);
for (int i = 1; i < n; i++) {
if (c[i]) {
t.update(in[i], in[i] + 1);
}
}
auto add = [&](int u) {
for (; u != -1; u = par[nxt[u]]) {
t.update(in[nxt[u]], in[u] + 1);
}
};
while (q--) {
int k;
cin >> k;
vector<int> d(k);
for (int i = 0; i < k; i++) {
cin >> d[i];
d[i]--;
}
set<int> v(d.begin(), d.end());
int leaves = cnt + k;
for (int x : v) {
if (deg[x] <= 1) {
leaves--;
}
}
if (leaves % 2 != 0) {
cout << "-1\n";
continue;
}
sort(d.begin(), d.end(), [&](int i, int j) {
return in[i] > in[j];
});
for (int u : d) {
if (deg[u] > 1) {
add(u);
}
deg[u]++;
}
int x = t.get(1, n);
int y = n - 1 - x;
int ans = 2 * y + x + k;
cout << ans << "\n";
reverse(d.begin(), d.end());
for (int u : d) {
deg[u]--;
if (deg[u] > 1) {
add(u);
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
128 ms |
2748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
1084 KB |
Output is correct |
2 |
Correct |
15 ms |
1108 KB |
Output is correct |
3 |
Correct |
85 ms |
11836 KB |
Output is correct |
4 |
Correct |
141 ms |
11012 KB |
Output is correct |
5 |
Correct |
137 ms |
14760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
1876 KB |
Output is correct |
2 |
Correct |
17 ms |
1852 KB |
Output is correct |
3 |
Correct |
98 ms |
20684 KB |
Output is correct |
4 |
Correct |
165 ms |
21596 KB |
Output is correct |
5 |
Correct |
75 ms |
18720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
3408 KB |
Output is correct |
2 |
Correct |
46 ms |
2644 KB |
Output is correct |
3 |
Correct |
17 ms |
2536 KB |
Output is correct |
4 |
Correct |
13 ms |
3148 KB |
Output is correct |
5 |
Correct |
17 ms |
3412 KB |
Output is correct |
6 |
Correct |
62 ms |
3204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
7768 KB |
Output is correct |
2 |
Correct |
239 ms |
7680 KB |
Output is correct |
3 |
Correct |
210 ms |
4072 KB |
Output is correct |
4 |
Correct |
255 ms |
7840 KB |
Output is correct |
5 |
Correct |
235 ms |
7780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
274 ms |
11832 KB |
Output is correct |
2 |
Correct |
132 ms |
15136 KB |
Output is correct |
3 |
Correct |
250 ms |
14284 KB |
Output is correct |
4 |
Correct |
148 ms |
15712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
128 ms |
2748 KB |
Output is correct |
3 |
Correct |
50 ms |
1084 KB |
Output is correct |
4 |
Correct |
15 ms |
1108 KB |
Output is correct |
5 |
Correct |
85 ms |
11836 KB |
Output is correct |
6 |
Correct |
141 ms |
11012 KB |
Output is correct |
7 |
Correct |
137 ms |
14760 KB |
Output is correct |
8 |
Correct |
41 ms |
1876 KB |
Output is correct |
9 |
Correct |
17 ms |
1852 KB |
Output is correct |
10 |
Correct |
98 ms |
20684 KB |
Output is correct |
11 |
Correct |
165 ms |
21596 KB |
Output is correct |
12 |
Correct |
75 ms |
18720 KB |
Output is correct |
13 |
Correct |
89 ms |
3408 KB |
Output is correct |
14 |
Correct |
46 ms |
2644 KB |
Output is correct |
15 |
Correct |
17 ms |
2536 KB |
Output is correct |
16 |
Correct |
13 ms |
3148 KB |
Output is correct |
17 |
Correct |
17 ms |
3412 KB |
Output is correct |
18 |
Correct |
62 ms |
3204 KB |
Output is correct |
19 |
Correct |
62 ms |
7768 KB |
Output is correct |
20 |
Correct |
239 ms |
7680 KB |
Output is correct |
21 |
Correct |
210 ms |
4072 KB |
Output is correct |
22 |
Correct |
255 ms |
7840 KB |
Output is correct |
23 |
Correct |
235 ms |
7780 KB |
Output is correct |
24 |
Correct |
274 ms |
11832 KB |
Output is correct |
25 |
Correct |
132 ms |
15136 KB |
Output is correct |
26 |
Correct |
250 ms |
14284 KB |
Output is correct |
27 |
Correct |
148 ms |
15712 KB |
Output is correct |
28 |
Correct |
182 ms |
7172 KB |
Output is correct |
29 |
Correct |
204 ms |
14484 KB |
Output is correct |
30 |
Correct |
108 ms |
14788 KB |
Output is correct |
31 |
Correct |
154 ms |
21636 KB |
Output is correct |
32 |
Correct |
236 ms |
7816 KB |
Output is correct |
33 |
Correct |
184 ms |
12352 KB |
Output is correct |
34 |
Correct |
197 ms |
15368 KB |
Output is correct |
35 |
Correct |
229 ms |
15216 KB |
Output is correct |