#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);
}
int ans = 2 * (N - 1);
vector<int> v(N), par(N, -1);
auto dfs = [&](auto dfs, int x) -> void {
if (deg[x] == 1) {
v[x] = 1;
}
for (int y : adj[x]) {
if (y == par[x]) {
continue;
}
par[y] = x;
dfs(dfs, y);
v[x] ^= v[y];
}
ans -= v[x];
};
dfs(dfs, 0);
while (Q--) {
int D;
cin >> D;
vector<int> a(D);
for (int &p : a) {
cin >> p;
p--;
if (deg[p]++ != 1) {
int x = p;
while (x != -1) {
ans += v[x] == 0 ? -1 : 1;
v[x] ^= 1;
x = par[x];
}
}
}
if (v[0] == 1) {
cout << -1 << "\n";
} else {
cout << ans + D << "\n";
}
for (int p : a) {
if (--deg[p] != 1) {
int x = p;
while (x != -1) {
ans += v[x] == 0 ? -1 : 1;
v[x] ^= 1;
x = par[x];
}
}
}
}
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
24 ms |
1728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
852 KB |
Output is correct |
2 |
Correct |
6 ms |
852 KB |
Output is correct |
3 |
Correct |
18 ms |
7384 KB |
Output is correct |
4 |
Correct |
24 ms |
5632 KB |
Output is correct |
5 |
Correct |
25 ms |
7656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
833 ms |
1108 KB |
Output is correct |
2 |
Correct |
801 ms |
1108 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
11600 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1079 ms |
2004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
4940 KB |
Output is correct |
2 |
Correct |
38 ms |
4744 KB |
Output is correct |
3 |
Correct |
22 ms |
2528 KB |
Output is correct |
4 |
Correct |
37 ms |
4740 KB |
Output is correct |
5 |
Correct |
36 ms |
4728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
7532 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
8720 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
24 ms |
1728 KB |
Output is correct |
3 |
Correct |
6 ms |
852 KB |
Output is correct |
4 |
Correct |
6 ms |
852 KB |
Output is correct |
5 |
Correct |
18 ms |
7384 KB |
Output is correct |
6 |
Correct |
24 ms |
5632 KB |
Output is correct |
7 |
Correct |
25 ms |
7656 KB |
Output is correct |
8 |
Correct |
833 ms |
1108 KB |
Output is correct |
9 |
Correct |
801 ms |
1108 KB |
Output is correct |
10 |
Execution timed out |
1086 ms |
11600 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |