#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;
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];
}
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) {
int x = p;
while (x != -1) {
cnt += v[x] == 0 ? 1 : -1;
v[x] ^= 1;
x = par[x];
}
}
}
if (v[0] == 1) {
cout << -1 << "\n";
} else {
cout << 2 * (N - 1) - cnt + D << "\n";
}
for (int p : a) {
if (--deg[p] != 1) {
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 |
316 KB |
Output is correct |
2 |
Correct |
20 ms |
2012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1276 KB |
Output is correct |
2 |
Correct |
6 ms |
1224 KB |
Output is correct |
3 |
Correct |
21 ms |
7764 KB |
Output is correct |
4 |
Correct |
22 ms |
5932 KB |
Output is correct |
5 |
Correct |
37 ms |
8056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
803 ms |
1536 KB |
Output is correct |
2 |
Correct |
816 ms |
1536 KB |
Output is correct |
3 |
Execution timed out |
1061 ms |
12080 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1067 ms |
2340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
5360 KB |
Output is correct |
2 |
Correct |
45 ms |
6024 KB |
Output is correct |
3 |
Correct |
24 ms |
3420 KB |
Output is correct |
4 |
Correct |
37 ms |
5984 KB |
Output is correct |
5 |
Correct |
37 ms |
5992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
7876 KB |
Output is correct |
2 |
Execution timed out |
1081 ms |
10188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
20 ms |
2012 KB |
Output is correct |
3 |
Correct |
8 ms |
1276 KB |
Output is correct |
4 |
Correct |
6 ms |
1224 KB |
Output is correct |
5 |
Correct |
21 ms |
7764 KB |
Output is correct |
6 |
Correct |
22 ms |
5932 KB |
Output is correct |
7 |
Correct |
37 ms |
8056 KB |
Output is correct |
8 |
Correct |
803 ms |
1536 KB |
Output is correct |
9 |
Correct |
816 ms |
1536 KB |
Output is correct |
10 |
Execution timed out |
1061 ms |
12080 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |