D. Book of Evil

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 n, m 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.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int N,m,num;
bool b[100005];
int dis1[100005],dis2[100005];
int maxx;
int node1,node2;
vector<vector<int> > v;
void inital(){memset(b,0,sizeof(b));v.clear();maxx = 0;memset(dis1,0,sizeof(dis1));memset(dis2,0,sizeof(dis2));
}
void dfs(int now,int fa){//cout<<now<<" "<<dis1[now]<<endl;for(int i=0;i<v[now].size();i++){int next = v[now][i];if(next!=fa){dis1[next] = dis1[now] + 1;dfs(next,now);if(b[next]&&dis1[next]>maxx){maxx = dis1[next];node1 = next;}}}
}
void getdis2(int now,int fa){for(int i=0;i<v[now].size();i++){int next = v[now][i];if(next!=fa){dis2[next] = dis2[now] + 1;getdis2(next,now);}}
}
int main(){//cin >> N >> m >> num;inital();scanf("%d%d%d",&N,&m,&num);v.resize(N+5);for(int i=0;i<m;i++){int now;scanf("%d",&now);b[now] = 1;}for(int i=1;i<N;i++){int a,b;scanf("%d%d",&a,&b);v[a].push_back(b);v[b].push_back(a);}dfs(1,-1);//cout<<"!!"<<endl;maxx = 0;memset(dis1,0,sizeof(dis1));node2 = node1;dfs(node2,-1);//cout<<node1<<" "<<node2<<endl;getdis2(node1,-1);int ans = 0;//cout<<node1<<" "<<node2<<endl;for(int i=1;i<=N;i++){if(dis1[i]<=num&&dis2[i]<=num)ans++;}printf("%d\n",ans);
}

cordeforces 337D Book of Evil相关推荐

  1. codeforces 337D Book of Evil(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/           --by fraud Book of Evil Paladin Manao caught the tr ...

  2. CodeForces 337D Book of Evil(双向dfs)

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  3. Codeforces 337D Book of Evil:树的直径【结论】

    题目链接:http://codeforces.com/problemset/problem/337/D 题意: 给你一棵树,n个节点. 如果一个节点处放着"罪恶之书",那么它会影响 ...

  4. 动态规划总结与题目分类

    源博客链接:http://blog.csdn.net/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少 ...

  5. 『ACM-算法-动态规划』初识DP动态规划算法

    一.多阶段决策过程的最优化问题 在现实生活中,有类活 动的过程,由于 它的特殊性,可将过程分成若干个互相阶段.在它的每一阶段都需要作出决策,从而使整个过程达到最好的活动效果.当阶段决策的选取不是任意确 ...

  6. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

  7. 《动态规划》— 动态规划分类

    动态规划(英语:Dynamic programming,DP)是一种在数学.计算机科学和经济学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法. 动态规划常常适用于有重叠子问题和最 ...

  8. 转:动态规划题目分类

    https://blog.csdn.net/cc_again/article/details/25866971 一.简单基础dp 这类dp主要是一些状态比较容易表示,转移方程比较好想,问题比较基本常见 ...

  9. 【DP专辑】ACM动态规划总结

    转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 ...

最新文章

  1. 万年历插件软件测试,万年历软件测试报告
  2. linux高级运维要会的,linux高级运维必会命令
  3. C语言经典例99-文件内容合并和排序
  4. C++ Primer 5th笔记(6)chapter6 函数:函数指针
  5. 由PLC未授权访问引发的工程重置漏洞
  6. bootstrap源码学习:辅助(1)
  7. ArcEngine 渲染的使用【转载】
  8. 2021-08-14
  9. 北京航空航天大学经管学院《量化交易与大数据金融》课程实验 :自选至少5支基金,和一个大盘指数,比较这5支基金的信息比率
  10. Linux下Qt编译出现“cannot find -lGL
  11. 狄利克雷卷积学习记录
  12. 分解为具有无损连接性和依赖保持性的3NF的方法以及例子
  13. mb计算机单位,mb是什么单位
  14. 萌新必看—10种前端存储哪家强,一文读尽!
  15. [导入]阿里妈妈广告牌生成器
  16. 《三国演义》与结巴(jieba)的碰撞
  17. 每日一threeJS(1)
  18. IT行业能干到多少岁?
  19. VS Code 遇上 Java丨第一章:配置 Java 开发环境
  20. 先进的分销管理系统(ADMS)行业调研报告 - 市场现状分析与发展前景预测

热门文章

  1. 2022年下半年系统集成项目管理工程师综合知识真题及答案解析
  2. matlab 创建简单的深度学习网络用于分类
  3. 基于JAVA社区管理系统计算机毕业设计源码+系统+lw文档+部署
  4. screw 一颗螺丝钉的使命
  5. SDL Trados Studio 2021 project templates for Microsoft Visual Studio 2022
  6. ListBox控件的使用
  7. python怎么制作游戏修改器_使用python编写游戏修改器
  8. MFC关于进程使用:创建、关闭及查询进程
  9. 解决【安装 Adobe AIR 时出错。管理员可能不允许安装此软件。请与管理员联系。】
  10. Illegal char <:> at index 4