mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 13:38:48 +00:00
F - Vlad and Unfinished Business
https://codeforces.com/contest/1675/submission/156007191
This commit is contained in:
parent
eb6c31f605
commit
bd187dd5e6
58
Codeforces/1675/F/F.cpp
Normal file
58
Codeforces/1675/F/F.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 2e5 + 5;
|
||||
|
||||
int t, n, k, x, y, a[N], siz[N], dis[N], ans;
|
||||
std::vector<int> g[N];
|
||||
|
||||
void dfs(int u, int fa) {
|
||||
for (int v : g[u]) {
|
||||
if (v == fa) continue;
|
||||
dis[v] = dis[u] + 1;
|
||||
dfs(v, u);
|
||||
siz[u] += siz[v];
|
||||
ans += !!siz[v];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
|
||||
cin >> t;
|
||||
|
||||
while (t--) {
|
||||
ans = 0;
|
||||
|
||||
cin >> n >> k >> x >> y;
|
||||
|
||||
siz[y] = 1;
|
||||
|
||||
for (int i = 1; i <= k; i++) {
|
||||
cin >> a[i];
|
||||
siz[a[i]] = 1;
|
||||
}
|
||||
|
||||
for (int i = 1, u, v; i < n; i++) {
|
||||
cin >> u >> v;
|
||||
|
||||
g[u].push_back(v);
|
||||
g[v].push_back(u);
|
||||
}
|
||||
|
||||
dfs(x, -1);
|
||||
|
||||
cout << ans * 2 - dis[y] << endl;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
g[i].clear();
|
||||
dis[i] = siz[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user