#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 1e5 + 69;
pair<int, int> st[4 * N];
int n, q, ch[N], par[N], leaf[N], is[N], dep[N];
int tin[N], timer, top[N], sz[N], lzy[4 * N];
vector<int> g[N];
void push(int v) {
if (lzy[v] % 2 == 1) {
lzy[v * 2] += lzy[v];
lzy[v * 2 + 1] += lzy[v];
swap(st[v * 2].f, st[v * 2].s);
swap(st[v * 2 + 1].f, st[v * 2 + 1].s);
lzy[v] = 0;
}
}
void dfs(int v, int p) {
par[v] = p;
sz[v] = 1;
if (g[v].size() == 1)
leaf[v] = ch[v] = is[v] = 1;
for (auto u : g[v]) {
if (u == p)
continue;
dep[u] = dep[v] + 1;
dfs(u, v);
ch[v] += ch[u];
sz[v] += sz[u];
}
}
void modify(int v, int tl, int tr, int p, int x) {
if (tl == tr) {
if (x % 2 == 0)
st[v] = {0, 1};
else
st[v] = {1, 0};
}
else {
int mid = (tl + tr) / 2;
if (p <= mid)
modify(v * 2, tl, mid, p, x);
else
modify(v * 2 + 1, mid + 1, tr, p, x);
st[v] = {st[v * 2].f + st[v * 2 + 1].f, st[v * 2].s + st[v * 2 + 1].s};
}
}
void svapuj(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return;
else if (tl >= l && tr <= r) {
lzy[v]++;
swap(st[v].f, st[v].s);
}
else {
push(v);
int mid = (tl + tr) / 2;
svapuj(v * 2, tl, mid, l, r);
svapuj(v * 2 + 1, mid + 1, tr, l, r);
st[v] = {st[v * 2].f + st[v * 2 + 1].f, st[v * 2].s + st[v * 2 + 1].s};
}
}
pair<int, int> get(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return {0, 0};
else if (tl >= l && tr <= r)
return st[v];
else {
int mid = (tl + tr) / 2;
push(v);
auto ll = get(v * 2, tl, mid, l, r);
auto rr = get(v * 2 + 1, mid + 1, tr, l, r);
pair<int, int> res;
res.f = {ll.f + rr.f};
res.s = {ll.s + rr.s};
return res;
}
}
void hld(int v, int p, int tp) {
int s = -1;
tin[v] = ++timer;
top[v] = tp;
modify(1, 1, n, tin[v], ch[v]);
for (auto u : g[v]) {
if (u == p)
continue;
if (s == -1 || sz[s] < sz[u])
s = u;
}
if (s != -1)
hld(s, v, tp);
for (auto u : g[v]) {
if (u == p || u == s)
continue;
hld(u, v, u);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 0);
hld(1, 0, 1);
int uk = ch[1];
while (q--) {
int t;
cin >> t;
int a[t];
map<int, int> cnt;
for (int i = 0;i < t;i++) {
cin >> a[i];
cnt[a[i]]++;
uk++;
if (leaf[a[i]]) {
leaf[a[i]]--;
uk--;
}
}
for (auto u : cnt) {
int ok = 0, v = u.f;
if (is[v] && u.s % 2 == 0)
ok = 1;
else if (!is[v] && u.s % 2 == 1)
ok = 1;
if (ok) {
while (v > 0) {
svapuj(1, 1, n, tin[top[v]], tin[v]);
v = par[top[v]];
}
}
}
if (uk % 2 == 1)
cout << "-1\n";
else {
pair<int, int> res;
res = get(1, 1, n, 2, n);
cout << res.f + 2 * res.s + t << '\n';
}
for (int i = 0;i < t;i++) {
uk--;
if (is[a[i]] && !leaf[a[i]]) {
uk++;
leaf[a[i]] = 1;
}
}
for (auto u : cnt) {
int ok = 0, v = u.f;
if (is[v] && u.s % 2 == 0)
ok = 1;
else if (!is[v] && u.s % 2 == 1)
ok = 1;
if (ok) {
while (v > 0) {
svapuj(1, 1, n, tin[top[v]], tin[v]);
v = par[top[v]];
}
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
77 ms |
9688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9304 KB |
Output is correct |
2 |
Correct |
13 ms |
9304 KB |
Output is correct |
3 |
Correct |
30 ms |
13304 KB |
Output is correct |
4 |
Correct |
59 ms |
14540 KB |
Output is correct |
5 |
Correct |
69 ms |
17092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
9564 KB |
Output is correct |
2 |
Correct |
14 ms |
9564 KB |
Output is correct |
3 |
Correct |
44 ms |
20444 KB |
Output is correct |
4 |
Correct |
75 ms |
22036 KB |
Output is correct |
5 |
Correct |
36 ms |
19332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
10076 KB |
Output is correct |
2 |
Correct |
30 ms |
9560 KB |
Output is correct |
3 |
Correct |
9 ms |
9308 KB |
Output is correct |
4 |
Correct |
10 ms |
9820 KB |
Output is correct |
5 |
Correct |
11 ms |
10104 KB |
Output is correct |
6 |
Correct |
43 ms |
9912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
11092 KB |
Output is correct |
2 |
Correct |
156 ms |
10840 KB |
Output is correct |
3 |
Correct |
119 ms |
10016 KB |
Output is correct |
4 |
Correct |
145 ms |
10844 KB |
Output is correct |
5 |
Correct |
147 ms |
11004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
13092 KB |
Output is correct |
2 |
Correct |
92 ms |
17296 KB |
Output is correct |
3 |
Correct |
125 ms |
17124 KB |
Output is correct |
4 |
Correct |
102 ms |
18348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
77 ms |
9688 KB |
Output is correct |
3 |
Correct |
12 ms |
9304 KB |
Output is correct |
4 |
Correct |
13 ms |
9304 KB |
Output is correct |
5 |
Correct |
30 ms |
13304 KB |
Output is correct |
6 |
Correct |
59 ms |
14540 KB |
Output is correct |
7 |
Correct |
69 ms |
17092 KB |
Output is correct |
8 |
Correct |
18 ms |
9564 KB |
Output is correct |
9 |
Correct |
14 ms |
9564 KB |
Output is correct |
10 |
Correct |
44 ms |
20444 KB |
Output is correct |
11 |
Correct |
75 ms |
22036 KB |
Output is correct |
12 |
Correct |
36 ms |
19332 KB |
Output is correct |
13 |
Correct |
39 ms |
10076 KB |
Output is correct |
14 |
Correct |
30 ms |
9560 KB |
Output is correct |
15 |
Correct |
9 ms |
9308 KB |
Output is correct |
16 |
Correct |
10 ms |
9820 KB |
Output is correct |
17 |
Correct |
11 ms |
10104 KB |
Output is correct |
18 |
Correct |
43 ms |
9912 KB |
Output is correct |
19 |
Correct |
94 ms |
11092 KB |
Output is correct |
20 |
Correct |
156 ms |
10840 KB |
Output is correct |
21 |
Correct |
119 ms |
10016 KB |
Output is correct |
22 |
Correct |
145 ms |
10844 KB |
Output is correct |
23 |
Correct |
147 ms |
11004 KB |
Output is correct |
24 |
Correct |
143 ms |
13092 KB |
Output is correct |
25 |
Correct |
92 ms |
17296 KB |
Output is correct |
26 |
Correct |
125 ms |
17124 KB |
Output is correct |
27 |
Correct |
102 ms |
18348 KB |
Output is correct |
28 |
Correct |
126 ms |
12272 KB |
Output is correct |
29 |
Correct |
87 ms |
16976 KB |
Output is correct |
30 |
Correct |
69 ms |
17600 KB |
Output is correct |
31 |
Correct |
76 ms |
23380 KB |
Output is correct |
32 |
Correct |
152 ms |
12368 KB |
Output is correct |
33 |
Correct |
110 ms |
16208 KB |
Output is correct |
34 |
Correct |
120 ms |
17648 KB |
Output is correct |
35 |
Correct |
119 ms |
17520 KB |
Output is correct |