#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 100005;
int leaves;
bool leaf[N];
vector<int> g[N];
void dfs (int v, int p) {
if (g[v].size() == 1) {
leaves++;
leaf[v] = true;
}
for (int u : g[v]) {
if (u != p) {
dfs(u, v);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
while (q--) {
int d;
cin >> d;
vector<int> nodes(d);
for (int i = 0; i < d; ++i) {
cin >> nodes[i];
}
int ans = leaves;
int tleaves = leaves + d;
sort(nodes.begin(), nodes.end());
for (int i = 0; i < d; ++i) {
int j = i;
while (j + 1 < d && nodes[j + 1] == nodes[j]) {
j++;
}
if (leaf[nodes[i]]) {
tleaves--;
ans--;
}
int ch = j - i + 1;
int match_together = ch - (2 - (ch % 2));
ans += match_together;
ans += (ch - match_together) * 2;
i = j;
}
if (tleaves % 2) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3460 KB |
Output is correct |
2 |
Correct |
10 ms |
3456 KB |
Output is correct |
3 |
Correct |
19 ms |
7036 KB |
Output is correct |
4 |
Correct |
23 ms |
6588 KB |
Output is correct |
5 |
Correct |
34 ms |
7844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
3796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
4400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
6316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
8296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |