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
| #include <iostream>
using namespace std;
const int maxn = 100005; int n, m, k; int t_point[maxn], head[maxn], dist_1[maxn], dist_2[maxn], dist_3[maxn]; bool isImPoint[maxn];
struct edge { int to, next; } g[maxn << 1];
int ecnt = 2;
void add_edge(int u, int v) { g[ecnt] = (edge) {v, head[u]}; head[u] = ecnt ++; }
void dfs(int u, int fa, int P) { for (int e = head[u]; e != 0; e = g[e].next) { int v = g[e].to; if (v == fa) continue; if (P == 1) dist_1[v] = dist_1[u] + 1; if (P == 2) dist_2[v] = dist_2[u] + 1; if (P == 3) dist_3[v] = dist_3[u] + 1; dfs(v, u, P); } }
int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i ++) { cin >> t_point[i]; isImPoint[t_point[i]] = true; } for (int i = 1; i < n; i ++) { int u, v; cin >> u >> v; add_edge(u, v); add_edge(v, u); } dfs(1, 0, 1); int node_first = 0; for (int i = 1; i <= m; i ++) if (dist_1[t_point[i]] >= dist_1[node_first]) node_first = t_point[i]; dfs(node_first, 0, 2); int node_second = 0; for (int i = 1; i <= m; i ++) if (dist_2[t_point[i]] >= dist_2[node_second]) node_second = t_point[i]; dfs(node_second, 0, 3); int cnt = 0; for (int i = 1; i <= n; i ++) { if (isImPoint[i]) continue; if (dist_2[i] <= k && dist_3[i] <= k) cnt ++; } cout << cnt << endl; return 0; }
|