#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<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
while (Q--) {
int D;
cin >> D;
for (int i = 0; i < D; i++) {
int p;
cin >> p; p--;
adj.push_back({p});
adj[p].push_back(N + i);
}
int leaves = 0;
for (int i = 0; i < N + D; i++) {
leaves += adj[i].size() == 1;
}
if (leaves % 2 == 1) {
cout << -1 << "\n";
} else {
int ans = 0;
auto dfs = [&](auto dfs, int x, int p) -> int {
int s = adj[x].size() == 1;
for (int y : adj[x]) {
if (y == p) {
continue;
}
int t = dfs(dfs, y, x);
ans += 2 - t % 2;
s += t;
}
return s;
};
dfs(dfs, 0, -1);
cout << ans << "\n";
}
adj.resize(N);
for (int i = 0; i < N; i++) {
if (!adj[i].empty() && adj[i].back() >= N) {
adj[i].pop_back();
}
}
}
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1076 ms |
3968 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
5388 KB |
Output is correct |
2 |
Correct |
11 ms |
5376 KB |
Output is correct |
3 |
Correct |
21 ms |
8644 KB |
Output is correct |
4 |
Correct |
31 ms |
11700 KB |
Output is correct |
5 |
Correct |
42 ms |
11168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
7216 KB |
Output is correct |
2 |
Correct |
14 ms |
7268 KB |
Output is correct |
3 |
Correct |
27 ms |
9316 KB |
Output is correct |
4 |
Correct |
41 ms |
13464 KB |
Output is correct |
5 |
Correct |
24 ms |
8512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
183 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1072 ms |
5424 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1082 ms |
8148 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1076 ms |
3968 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |