#787 (div.3) F
作者:
昊子
,
2022-05-06 20:14:42
,
所有人可见
,
阅读 239
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define endl '\n'
int n, k, x, y, ans;
int a[200005];
int ne[400005], e[400005], h[200005], idx;
bool st[200005];
int pre[200005];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u, int fa, int len){
st[u] = false;
if(u == y && ans == -1)ans = len;
for(int i = h[u]; ~ i; i = ne[i]){
int j = e[i];
if(j == fa)continue;
dfs(j, u, len + 1);
pre[j] = u;
}
}
void solve(){
ans = -1;
memset(h, -1, sizeof h);
cin >> n >> k;
cin >> x >> y;
for(int i = 1; i <= k; ++ i)cin >> a[i];
for(int i = 0; i < n - 1; ++ i){
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
}
dfs(x, -1, 0);
int tmp = y;
st[x] = true;
while(tmp != x){
st[tmp] = true;
tmp = pre[tmp];
}
for(int i = 1; i <= k; ++ i){
int j = a[i];
int res = 0;
while(true){
if(st[j] == false){
res ++;
st[j] = true;
j = pre[j];
}
else break;
}
ans += 2 * res;
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int _;
cin >> _;
while(_ --){
solve();
}
return 0;
}