#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 10;
int n, q, ans;
set <int> g[N];
int sz[N];
void dfs(int u, int p) {
if (g[u].size() <= 1 && p) {
sz[u] = 1;
return;
}
sz[u] = 0;
for (auto v: g[u]) {
if (v == p) continue;
dfs(v, u);
sz[u] += sz[v];
ans += (sz[v] % 2 ? 1 : 2);
}
}
void solve() {
int m;
cin >> m;
for (int i = 1; i <= m; ++i) {
int u;
cin >> u;
g[u].insert(i + n);
g[i + n].insert(u);
}
int leaves = 0;
for (int i = 1; i <= n + m; ++i)
if (g[i].size() <= 1) ++leaves;
if (leaves % 2 == 0) {
dfs(1, 0);
cout << ans << '\n';
} else cout << -1 << '\n';
for (int u = n + 1; u <= n + m; ++u) {
for (auto v: g[u]) g[v].erase(u);
g[u].clear();
}
ans = 0;
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].insert(v);
g[v].insert(u);
}
while (q--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1086 ms |
7852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
13508 KB |
Output is correct |
2 |
Correct |
44 ms |
13188 KB |
Output is correct |
3 |
Runtime error |
81 ms |
29740 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
14136 KB |
Output is correct |
2 |
Correct |
59 ms |
13412 KB |
Output is correct |
3 |
Runtime error |
75 ms |
30168 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
332 ms |
8456 KB |
Output is correct |
2 |
Correct |
258 ms |
7728 KB |
Output is correct |
3 |
Correct |
180 ms |
7172 KB |
Output is correct |
4 |
Correct |
16 ms |
7640 KB |
Output is correct |
5 |
Correct |
301 ms |
7924 KB |
Output is correct |
6 |
Correct |
387 ms |
8352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1075 ms |
12344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1085 ms |
16068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1086 ms |
7852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |