#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 4e5 + 69;
int n, q, ch[N], par[N];
vector<int> g[N];
void dfs(int v, int p) {
par[v] = p;
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 0);
while (q--) {
int t;
cin >> t;
int a[t];
for (int i = 0;i < t;i++) {
cin >> a[i];
g[a[i]].push_back(n + i + 1);
g[n + i + 1].push_back(a[i]);
}
function<void(int, int)> calc = [&](int v, int p) {
if (g[v].size() == 1)
ch[v] = 1;
else
ch[v] = 0;
for (auto u : g[v]) {
if (u == p)
continue;
calc(u, v);
ch[v] += ch[u];
}
};
calc(1, 0);
int uk = 0;
for (int i = 1;i <= n + t;i++)
uk += (g[i].size() == 1);
if (uk % 2 == 1)
cout << "-1\n";
else {
int ans = 0;
for (int i = 2;i <= n + t;i++)
ans += (ch[i] % 2 == 0 ? 2 : 1);
cout << ans << '\n';
}
for (int i = 0;i < t;i++) {
g[a[i]].pop_back();
assert(g[n + i + 1].size() == 1);
g[n + i + 1].pop_back();
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Execution timed out |
1082 ms |
13852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16216 KB |
Output is correct |
2 |
Correct |
18 ms |
16708 KB |
Output is correct |
3 |
Correct |
22 ms |
17260 KB |
Output is correct |
4 |
Correct |
47 ms |
19532 KB |
Output is correct |
5 |
Correct |
46 ms |
20684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16728 KB |
Output is correct |
2 |
Correct |
16 ms |
16732 KB |
Output is correct |
3 |
Correct |
38 ms |
25176 KB |
Output is correct |
4 |
Correct |
53 ms |
27740 KB |
Output is correct |
5 |
Correct |
35 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
14688 KB |
Output is correct |
2 |
Correct |
129 ms |
13912 KB |
Output is correct |
3 |
Correct |
195 ms |
13660 KB |
Output is correct |
4 |
Correct |
219 ms |
14424 KB |
Output is correct |
5 |
Correct |
201 ms |
14684 KB |
Output is correct |
6 |
Correct |
234 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1057 ms |
14940 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1016 ms |
15952 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Execution timed out |
1082 ms |
13852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |