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
  1. 6 2 3
    1 2
    1 5
    2 3
    3 4
    4 5
    5 6
output
  1. 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]

统计个数

  1. #include<cstdio>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. const int maxn=1e5+;
  7. const int inf=0x3f3f3f3f;
  8.  
  9. inline int max(int a,int b)
  10. {
  11. return a>b?a:b;
  12. }
  13.  
  14. int dp[maxn][];
  15. int siz[maxn];
  16. bool use[maxn];
  17. int son[maxn];
  18. struct Edge
  19. {
  20. int to,next;
  21. };
  22. Edge edge[maxn<<];
  23. int head[maxn];
  24. int tot=;
  25.  
  26. void addedge(int u,int v)
  27. {
  28. edge[tot].to=v;
  29. edge[tot].next=head[u];
  30. head[u]=tot++;
  31. }
  32.  
  33. void solve(int ,int ,int );
  34. void dfs0(int ,int );
  35. void dfs1(int ,int );
  36. void dfs2(int ,int ,int );
  37.  
  38. int main()
  39. {
  40. memset(head,-,sizeof head);
  41. memset(use,false,sizeof use);
  42. memset(son,-,sizeof son);
  43. int n,m,d;
  44. scanf("%d %d %d",&n,&m,&d);
  45. for(int i=;i<=m;i++){
  46. int u;
  47. scanf("%d",&u);
  48. use[u]=true;
  49. }
  50.  
  51. for(int i=;i<n;i++){
  52. int u,v;
  53. scanf("%d %d",&u,&v);
  54. addedge(u,v);
  55. addedge(v,u);
  56. }
  57. solve(n,m,d);
  58. return ;
  59. }
  60.  
  61. void solve(int n,int m,int d)
  62. {
  63. memset(dp,,sizeof dp);
  64. dfs0(,-);
  65. dfs1(,-);
  66. dfs2(,-,m);
  67.  
  68. int ans=;
  69. for(int i=;i<=n;i++){
  70. if(max(dp[i][],dp[i][])<=d)
  71. ans++;
  72. }
  73. printf("%d\n",ans);
  74. return ;
  75. }
  76.  
  77. void dfs0(int u,int pre)
  78. {
  79. if(use[u])
  80. siz[u]=;
  81. else
  82. siz[u]=;
  83. for(int i=head[u];~i;i=edge[i].next){
  84. int v=edge[i].to;
  85. if(v==pre)
  86. continue;
  87. dfs0(v,u);
  88. if(siz[v]){
  89. dp[u][]=max(dp[u][],dp[v][]+);
  90. siz[u]+=siz[v];
  91. if(son[u]==-||dp[v][]>dp[son[u]][])
  92. son[u]=v;
  93. }
  94. }
  95. }
  96.  
  97. void dfs1(int u,int pre)
  98. {
  99. for(int i=head[u];~i;i=edge[i].next){
  100. int v=edge[i].to;
  101. if(v==pre)
  102. continue;
  103. if(siz[v]){
  104. dfs1(v,u);
  105. if(v==son[u]){
  106. continue;
  107. }
  108. else{
  109. dp[u][]=max(dp[u][],dp[v][]+);
  110. }
  111. }
  112. }
  113. }
  114.  
  115. void dfs2(int u,int pre,int m)
  116. {
  117. for(int i=head[u];~i;i=edge[i].next){
  118. int v=edge[i].to;
  119. if(v==pre)
  120. continue;
  121. if(m>siz[v]){
  122. if(v==son[u])
  123. dp[v][]=max(dp[u][],dp[u][])+;
  124. else
  125. dp[v][]=max(dp[u][],dp[u][])+;
  126. }
  127. dfs2(v,u,m);
  128. }
  129. }

CF 337D Book of Evil 树形DP 好题的更多相关文章

  1. codeforces 337D Book of Evil (树形dp)

    题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...

  2. codeforce 337D Book of Evil ----树形DP&bfs&树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  3. POJ 1155 TELE 背包型树形DP 经典题

    由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...

  4. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  5. 51nod 1353 树 | 树形DP经典题!

    51nod 1353 树 | 树形DP好题! 题面 切断一棵树的任意条边,这棵树会变成一棵森林. 现要求森林中每棵树的节点个数不小于k,求有多少种切法. 数据范围:\(n \le 2000\). 题解 ...

  6. P2016 战略游戏——树形DP大水题

    P2016 战略游戏 树形DP 入门题吧(现在怎么是蓝色标签搞不懂): 注意是看见每一条边而不是每一个点(因为这里错了好几次): #include<cstdio> #include< ...

  7. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  8. CF EDU 1101D GCD Counting 树形DP + 质因子分解

    CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...

  9. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

随机推荐

  1. CentOS搭建LNMP环境

    安装开发工具包: yum groupinstall -y "Development Tools*" 50多个,安装了好久…… 下载Nginx: http://nginx.org/e ...

  2. hdu1827 强连通

    题意:一个人需要联系其他所有人,已知他自己联系每个人的花费,并且他可以联系某个人再让他联系他能联系到的人,给出一系列关系表示 A 能够联系 B.问他最少需要联系多少人,花费多少钱 首先,建成一个有向图 ...

  3. UVA10305 拓扑序

    题意:给出多个任务,以及一系列任务的关系表示某个任务必须在某个任务前完成,问一个合理的任务完成顺序 拓扑序的裸题,一开始用大白书的写法,后来发现并不好用,就换了BFS又A了一遍. 原: #includ ...

  4. c语言开发手机通讯录

    // //  main.c //  手机通讯录 // //  Created by Kevin-Dfg on 16/4/19. //  Copyright © 2016年 Kevin-Dfg. All ...

  5. ARM1138@库函数速查

    1. GPIO库函数 可实现的功能: 获得/设置指定管脚的方向(输入.输出)和模式(硬件控制) 获取/设置指定管脚的配置(驱动强度2/4/8/8_SCmA.管脚模式:推挽(弱上拉/弱下拉)/开漏(弱上 ...

  6. Java——日期格式

     /* * 日期对象和毫秒值之间的转换. * * 毫秒值--->日期对象: *  1.通过Date对象的构造方法new Date(timeMillis) *  2.还可以通过setTime设 ...

  7. HTML中特殊字符和与之对应的ASCII代码

    ASCII代码是说明了在html中每个特殊字符的属性以及字符的简要说明.在使用html时,如何把ASCII代码添加到网页中.例如版权符号'©'在html中可以通过 "©"来显示. ...

  8. (转) An overview of gradient descent optimization algorithms

    An overview of gradient descent optimization algorithms Table of contents: Gradient descent variants ...

  9. Java 学习

    effective+java第三版 2016-09-23 15:25 effective+java第三版 相关问答 EffectiveJava第28条中关于泛型的一个疑问? @又名耶稣谢邀 一.先说说 ...

  10. C#实现通过模板自动创建Word文档的方法

    原文地址:http://www.jb51.net/article/55332.htm   本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧.分享给大家供大家参考.具体实现方法 ...