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 61 62 63 64 65 66 67 68 69
| #include <iostream> #include <cstdio> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <map> #include <set> #include <cmath> #include <cstring> #include <string>
#define LL long long #define INF 0x3f3f3f3f #define eps 1e-8
using namespace std; struct edge{ int v, next; }edges[400005]; int n, k; int head[200005], tot = 0, vis[200005]; int sz[200005]; void Add(int u, int v){ edges[tot].v = v; edges[tot].next = head[u]; head[u] = tot++; } void dfs(int u, int fa){ sz[u] = vis[u]; for (int i = head[u]; i != -1; i = edges[i].next){ int v = edges[i].v; if (v == fa) continue; dfs(v, u); sz[u] += sz[v]; } } LL ans = 0; void dfs2(int u, int fa){ for (int i = head[u]; i != -1; i = edges[i].next){ int v = edges[i].v; if (v == fa) continue; dfs2(v, u); ans += min(sz[v], 2 * k - sz[v]); } } int main(){ #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif memset(head, -1, sizeof(head)); scanf("%d%d", &n, &k); for (int i = 1; i <= 2 * k; i++){ int x; scanf("%d", &x); vis[x] = 1; } for (int i = 1; i < n; i++){ int u, v; scanf("%d%d", &u, &v); Add(u, v); Add(v, u); } dfs(1, 0); dfs2(1, 0); printf("%I64d\n", ans); return 0; }
|