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
| #define MAX 200005 int a[MAX], siz[MAX]; vector<int> G[MAX]; int n, m; ll ans = 0; void dfs(int u, int fa) { if (a[u]) { siz[u] = 1; } for (int v : G[u]) { if (v == fa) { continue; } dfs(v, u); siz[u] += siz[v]; } } void dfs1(int u, int fa) { for (int v : G[u]) { if (v == fa) { continue; } dfs1(v, u); if (siz[v] && (siz[1] - siz[v])) { ans++; } } } signed main() { IOS; cin >> n >> m; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= m; i++) { int x; cin >> x; a[x]++; } dfs(1, -1); dfs1(1, -1); cout << ans + 1 << endl; return 0; }
|