问题大意:
给出一棵n 个节点的树,每条边距离为1
其中有m 个节点受到了(来自东方的)神秘力量的影响
如果点x 有神秘力量来源,则距离x 小于等于d 的点均会被影响
神秘力量来源只有一个,问有几个点可能有神秘力量来源
一个节点可能有神秘力量来源,当且仅当它与所有受影响点距离均小于等于d
首先考虑求子树内的最大距离
设为点x 到子树中节点的最远距离,不难得出
要保证中有被神秘力量影响的节点
设为x 到非子树中节点的最远距离
对于非子树中的节点,有两种可能
- 来自兄弟节点
- 来自父节点
对于来自兄弟节点的转移,暴力枚举会超时
考虑用记录x 子节点中距离最大值
仅在遍历所有兄弟节点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
70
71
72
73
using namespace std;
const int N=100050;
int fa[N],son[N],f[N],g[N]; // subtree/not
int n,d,m,w[N],ans=0;
vector<int> e[N];
inline int read()
{
register int x=0,t=1;
register char ch=getchar();
while (ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if (ch=='-') t=-1,ch=getchar();
while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
void calc(int o,int pre)
{
if (w[o]) f[o]=0;
for(int i=0;i<e[o].size();i++)
{
int to=e[o][i];
if (to!=pre)
{
fa[to]=o;
calc(to,o);
if (f[to]!=-1)
{
f[o]=max(f[to]+1,f[o]);
if (!son[o]||f[to]>f[son[o]]) son[o]=to;
}
}
}
}
void dfs(int o,int pre)
{
if (w[o]) g[o]=0;
if (o!=pre)
{
if (g[pre]!=-1) g[o]=max(g[o],g[pre]+1);
if (o!=son[pre]) g[o]=max(g[o],f[son[pre]]+2);
for(int i=0;o==son[pre]&&i<e[pre].size();i++)
{
int to=e[pre][i];
if (to!=o&&to!=fa[pre]&&f[to]!=-1) g[o]=max(g[o],f[to]+2);
}
}
for(int i=0;i<e[o].size();i++)
{
int to=e[o][i];
if (to!=pre) dfs(to,o);
}
}
int main()
{
n=read(),m=read(),d=read();
for(int i=1;i<=m;i++) w[read()]=1;
for(int i=1;i<n;i++)
{
int u=read(),v=read();
e[u].push_back(v);
e[v].push_back(u);
}
memset(f,-1,sizeof(f));
memset(g,-1,sizeof(g));
calc(1,1);
dfs(1,1);
for(int i=1;i<=n;i++)
if (f[i]<=d&&g[i]<=d) ans++;
printf("%d\n",ans);
return 0;
}