-
Notifications
You must be signed in to change notification settings - Fork 87
/
035-01.cpp
48 lines (48 loc) · 957 Bytes
/
035-01.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<vector<int> > G(N);
for (int i = 0; i < N - 1; ++i) {
int A, B;
cin >> A >> B; --A, --B;
G[A].push_back(B);
G[B].push_back(A);
}
int Q;
cin >> Q;
for (int i = 0; i < Q; ++i) {
int verts;
cin >> verts;
vector<int> sel(verts);
for (int j = 0; j < verts; ++j) {
cin >> sel[j]; --sel[j];
}
vector<int> c(N);
for (int j = 0; j < verts; ++j) {
++c[sel[j]];
}
function<void(int, int)> tree_dp = [&](int pos, int pre) {
for (int i : G[pos]) {
if (i == pre) continue;
tree_dp(i, pos);
c[pos] += c[i];
}
};
tree_dp(sel[0], -1);
int need = 0;
for (int i = 0; i < N; ++i) {
if (i != sel[0] && c[i] != 0) {
++need;
}
}
cout << need << '\n';
}
return 0;
}