/*
Spring Cleaning
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<int> g[N];
int p[N], c[N], d[N], z[N], in[N], out[N];
int dfs1(int i) {
int s = 1, x = 0;
c[i] = -1;
for (int j : g[i])
if (p[i] != j) {
p[j] = i;
d[j] = d[i] + 1;
int t = dfs1(j);
if (x > t)
c[i] = j;
s += t;
}
return s;
}
void dfs2(int r, int i) {
static int t = 0;
in[i] = t++;
z[i] = r;
if (c[i] != -1)
dfs2(r, c[i]);
for (int j : g[i])
if (p[i] != j && c[i] != j)
dfs2(j, j);
out[i] = t - 1;
}
int t0[N * 4], t1[N * 4], lz[N * 4];
void build(int k, int l, int r) {
t0[k] = r - l + 1;
if (l != r) {
int m = (l + r) / 2;
build(k << 1 | 0, l, m), build(k << 1 | 1, m + 1, r);
}
}
void put(int k) {
swap(t0[k], t1[k]);
lz[k] ^= 1;
}
void update(int k, int l, int r, int ql, int qr) {
if (ql > r || qr < l)
return;
if (ql <= l && qr >= r)
put(k);
else {
int m = (l + r) / 2;
if (lz[k])
put(k << 1 | 0), put(k << 1 | 1), lz[k] = 0;
update(k << 1 | 0, l, m, ql, qr), update(k << 1 | 1, m + 1, r, ql, qr);
t0[k] = t0[k << 1 | 0] + t0[k << 1 | 1], t1[k] = t1[k << 1 | 0] + t1[k << 1 | 1];
}
}
int n;
void update(int i) {
while (i != -1) {
update(1, 0, n - 1, in[z[i]], in[i]);
i = p[z[i]];
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int q;
cin >> n >> q;
for (int h = 0; h < n - 1; h++) {
int i, j;
cin >> i >> j, i--, j--;
g[i].push_back(j), g[j].push_back(i);
}
int r = 0;
while (g[r].size() == 1)
r++;
p[r] = -1;
dfs1(r);
dfs2(r, r);
int l = 0;
build(1, 0, n - 1);
for (int i = 0; i < n; i++)
if (g[i].size() == 1)
update(i), l++;
while (q--) {
int d;
cin >> d;
vector<int> t(d), u;
for (int& i : t)
cin >> i, i--;
sort(t.begin(), t.end());
int l_ = l + d;
for (int i = 0, j; i < (int) t.size(); i = j) {
j = i;
while (j < (int) t.size() && t[i] == t[j])
j++;
if ((g[t[i]].size() > 1) ^ ((j - i) % 2 == 0))
update(t[i]), u.push_back(t[i]);
l_ -= g[t[i]].size() == 1;
}
cout << (l_ % 2 == 1 ? -1 : n + d + t0[1] - 2) << '\n';
for (int i : u)
update(i);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
271 ms |
5252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
3712 KB |
Output is correct |
2 |
Correct |
13 ms |
3668 KB |
Output is correct |
3 |
Correct |
65 ms |
12128 KB |
Output is correct |
4 |
Correct |
70 ms |
11244 KB |
Output is correct |
5 |
Correct |
97 ms |
13152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1072 ms |
4428 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1089 ms |
5504 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
9088 KB |
Output is correct |
2 |
Correct |
369 ms |
8912 KB |
Output is correct |
3 |
Correct |
247 ms |
6016 KB |
Output is correct |
4 |
Correct |
347 ms |
9124 KB |
Output is correct |
5 |
Correct |
354 ms |
8964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
640 ms |
13644 KB |
Output is correct |
2 |
Execution timed out |
1060 ms |
15312 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
271 ms |
5252 KB |
Output is correct |
3 |
Correct |
13 ms |
3712 KB |
Output is correct |
4 |
Correct |
13 ms |
3668 KB |
Output is correct |
5 |
Correct |
65 ms |
12128 KB |
Output is correct |
6 |
Correct |
70 ms |
11244 KB |
Output is correct |
7 |
Correct |
97 ms |
13152 KB |
Output is correct |
8 |
Execution timed out |
1072 ms |
4428 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |