-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathf.cpp
60 lines (55 loc) · 951 Bytes
/
f.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
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int T, N, K, X, Y, A[200000], M[200000], D[200000], S[200000];
vector<int> G[200000];
void dfs_sub(int v, int p, int d){
S[v] = M[v];
D[v] = d;
for(int c : G[v]){
if(c == p) continue;
dfs_sub(c, v, d+1);
S[v] += S[c];
}
}
int ans;
void dfs(int v, int p){
if(S[v] == 0) return;
ans++;
for(int c : G[v]){
if(c == p) continue;
dfs(c, v);
}
ans++;
}
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> T;
while(T--){
eat >> N >> K >> X >> Y;
X--, Y--;
for(int i = 0; i < N; i++){
G[i].clear();
M[i] = 0;
}
for(int i = 0; i < K; i++){
eat >> A[i];
A[i]--;
M[A[i]]++;
}
M[Y]++;
for(int i = 0; i < N-1; i++){
int u, v; eat >> u >> v;
u--, v--;
G[u].push_back(v);
G[v].push_back(u);
}
dfs_sub(X, -1, 0);
ans = -D[Y]-2;
dfs(X, -1);
moo << ans << endl;
}
}