Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.

The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.

Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.

Input

The first line contains three space-separated integers nm and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 ≤ pi ≤ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.

Output

Print a single number — the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.

Sample test(s)
input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
output
3
Note

Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.

题意:

1棵树,n个节点,编号为1~n,树的边权都是1再

给出m,d,然后有m个数

已知在某一个节点上有一个武器,与这个武器距离在d以内的节点都会受到辐射

现在已经知道有m个节点受到了辐射,问武器可能在的节点的个数

即求:这棵树上到这m个节点的距离都<=d的节点的个数。

树形DP,开始不知道怎么DP,总想着暴力。

令tree(i)表示以节点i为根的子树

把这m个节点称之为辐射点

dp[i][1] 表示tree(i)中,与i距离最远的辐射点的距离

dp[i][2] 表示tree(i)中,与i距离第二远的辐射点的距离(求dp[i][0]的时候需要用到)

dp[i][0] 表示整棵树-tree(i)中,与i距离最远的辐射点的距离

则与i距离最远的辐射点的距离=max(dp[i][1],dp[i][0])

若max(dp[i][0],dp[i][1])<=d,则节点i可能是武器的位置

则要求的就是满足max(dp[i][1],dp[i][0])<=d的i的个数

siz[i] 表示tree(i)中,辐射点的个数

son[i] 表示tree(i)中,dp[i][1]经过i的儿子节点son[i]

use[i] 表示节点i是不是辐射点

3次dfs,分别求出dp[i][1],dp[i][2],dp[i][0]

统计个数

 #include<cstdio>
#include<cstring> using namespace std; const int maxn=1e5+;
const int inf=0x3f3f3f3f; inline int max(int a,int b)
{
return a>b?a:b;
} int dp[maxn][];
int siz[maxn];
bool use[maxn];
int son[maxn];
struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot=; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void solve(int ,int ,int );
void dfs0(int ,int );
void dfs1(int ,int );
void dfs2(int ,int ,int ); int main()
{
memset(head,-,sizeof head);
memset(use,false,sizeof use);
memset(son,-,sizeof son);
int n,m,d;
scanf("%d %d %d",&n,&m,&d);
for(int i=;i<=m;i++){
int u;
scanf("%d",&u);
use[u]=true;
} for(int i=;i<n;i++){
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n,m,d);
return ;
} void solve(int n,int m,int d)
{
memset(dp,,sizeof dp);
dfs0(,-);
dfs1(,-);
dfs2(,-,m); int ans=;
for(int i=;i<=n;i++){
if(max(dp[i][],dp[i][])<=d)
ans++;
}
printf("%d\n",ans);
return ;
} void dfs0(int u,int pre)
{
if(use[u])
siz[u]=;
else
siz[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
dfs0(v,u);
if(siz[v]){
dp[u][]=max(dp[u][],dp[v][]+);
siz[u]+=siz[v];
if(son[u]==-||dp[v][]>dp[son[u]][])
son[u]=v;
}
}
} void dfs1(int u,int pre)
{
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
if(siz[v]){
dfs1(v,u);
if(v==son[u]){
continue;
}
else{
dp[u][]=max(dp[u][],dp[v][]+);
}
}
}
} void dfs2(int u,int pre,int m)
{
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
if(m>siz[v]){
if(v==son[u])
dp[v][]=max(dp[u][],dp[u][])+;
else
dp[v][]=max(dp[u][],dp[u][])+;
}
dfs2(v,u,m);
}
}

最新文章

  1. nginx添加 nginx_heath模块
  2. SQL Server在执行SQL语句时,表之间驱动顺序对性能的影响
  3. Oracle忘记密码的处理办法
  4. Codeforces Round #231 (Div. 2) E.Lightbulb for Minister
  5. mq安装参考
  6. ural 1113,jeep problem
  7. NSS_03 过滤器
  8. RedHat7上安装PHP
  9. WordPress nginx环境下开启多站点
  10. Inno Setup:获取isl中的多国语言字串
  11. 3027 - Corporative Network(并差集)
  12. 怎样设制 select 不可编辑 仅仅读
  13. java集合概念
  14. CI 路由设置
  15. cocos2dx - v2.3.3编辑器简单使用及不同分辨率适配
  16. C++与Lua交互之配置&amp;交互原理&amp;示例
  17. mysql存储过程 带参数 插入 操作
  18. Python第四天 流程控制 if else条件判断 for循环 while循环
  19. ByteArrayInputStream
  20. DOM元素加载之前执行的jQuery代码

热门文章

  1. 要将serviceLocator注入到任何类中
  2. 利用Web服务器网络打洞
  3. Meven笔记
  4. E:nth-child(n)实现奇偶匹配
  5. HttpWebRequest&#39;s Timeout and ReadWriteTimeout — What do these mean for the underlying TCP connection?
  6. 基于OkHttp的封装库TigerOkHttp的使用
  7. EasyUI之DataGrid使用
  8. ruby-gem
  9. 关于spring 3.0.5的 &lt;mvc:resources mapping=&quot;***&quot; location=&quot;***&quot;&gt;标签的使用
  10. dubbo远程调试运行