About
include <bits/stdc++.h>
using namespace std;
define ll long long
define ld long double
define SET(x, a) memset(x, a, sizeof x);
define mp make_pair
define pb push_back
define pii pair<int, int>
define pll pair<ll, ll>
define fi first
define se second
define pld pair<ld, ld>
const ll MOD = 1000000007; const ll INF = 1000000000; template <class T> inline void read(T &x) { char c; int flag = 1; while ((c = getchar()) < '0' || c > '9') if (c == '-') flag = -1; x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = x 10 + c - '0'; x = flag; return; } bool pho[100005]; int phorest; struct edge { int v, nxt; } E[200005]; int head[100005]; int n, m; int dep[100005]; int sum = 0; inline void dfs(int x, int fa, int lst) { if (pho[x]) { sum += abs(dep[lst] - dep[x]) 2; lst = x; } for (int i = head[x]; i != -1; i = E[i].nxt) { int v = E[i].v; if (v == fa) continue; dep[v] = dep[x] + 1; dfs(v, x, lst); } } int tot; inline void addedge(int x, int y) { E[tot] = (edge){y, head[x]}; head[x] = tot++; E[tot] = (edge){x, head[y]}; head[y] = tot++; } int main() { ios::sync_with_stdio(false); cin.tie(0); SET(head, -1); cin >> n >> m; for (int i = 1; i <= m; ++i) { int x; cin >> x; pho[x + 1] = 1; phorest = x + 1; } for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; x++, y++; addedge(x, y); } dfs(phorest, -1, 0); for (int i = 1; i <= n; ++i) if (dep[i] > dep[phorest] && pho[i]) phorest = i; SET(dep, 0); sum = 0; dfs(phorest, -1, 0); for (int i = 1; i <= n; ++i) if (dep[i] > dep[phorest] && pho[i]) phorest = i; cout << sum - dep[phorest] << endl; }