#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 cnt = 0, leaves = 0;
vector<int> v(N), par(N, -1);
auto dfs = [&](auto dfs, int x) -> void {
if (deg[x] == 1) {
v[x] = 1;
leaves++;
}
for (int y : adj[x]) {
if (y == par[x]) {
continue;
}
par[y] = x;
dfs(dfs, y);
v[x] ^= v[y];
}
cnt += 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) {
leaves++;
int x = p;
while (x != -1) {
cnt += v[x] == 0 ? 1 : -1;
v[x] ^= 1;
x = par[x];
}
}
}
if (leaves % 2 == 1) {
cout << -1 << "\n";
} else {
cout << 2 * (N - 1) - cnt + D<< "\n";
}
for (int p : a) {
if (--deg[p] != 1) {
leaves--;
int x = p;
while (x != -1) {
cnt += 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 |
18 ms |
1620 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 |
25 ms |
7320 KB |
Output is correct |
4 |
Correct |
23 ms |
5624 KB |
Output is correct |
5 |
Correct |
25 ms |
7720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
867 ms |
1236 KB |
Output is correct |
2 |
Correct |
872 ms |
1108 KB |
Output is correct |
3 |
Execution timed out |
1081 ms |
11988 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1068 ms |
2128 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
4972 KB |
Output is correct |
2 |
Correct |
45 ms |
4712 KB |
Output is correct |
3 |
Correct |
22 ms |
2516 KB |
Output is correct |
4 |
Correct |
37 ms |
4712 KB |
Output is correct |
5 |
Correct |
40 ms |
4744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
7500 KB |
Output is correct |
2 |
Execution timed out |
1081 ms |
8848 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 |
18 ms |
1620 KB |
Output is correct |
3 |
Correct |
6 ms |
852 KB |
Output is correct |
4 |
Correct |
6 ms |
852 KB |
Output is correct |
5 |
Correct |
25 ms |
7320 KB |
Output is correct |
6 |
Correct |
23 ms |
5624 KB |
Output is correct |
7 |
Correct |
25 ms |
7720 KB |
Output is correct |
8 |
Correct |
867 ms |
1236 KB |
Output is correct |
9 |
Correct |
872 ms |
1108 KB |
Output is correct |
10 |
Execution timed out |
1081 ms |
11988 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |