D. Book of Evil
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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

题意:找离所有给定的点的距离都不超过d的点的个数;

思路:树形DP;

找到每个点的距离最远的给定的点,由于给定的是一棵树,然后剩下的就是和http://www.cnblogs.com/zzuli2sjy/p/6232574.html这题一样;

 #include<stdio.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
typedef long long LL;
vector<int>vec[];
bool flag[];
void Init();
typedef struct node
{
int id1;
int cost1;
int id2;
int cost2;
} ss;
ss dp[];
bool ff[];
void dfs(int n);
void dfs2(int n);
int main(void)
{
int n,m,d;
while(scanf("%d %d %d",&n,&m,&d)!=EOF)
{
int x,y;
memset(ff,,sizeof(ff));
for(int i = ; i < m; i++)
scanf("%d",&x),ff[x] = true;
Init();
for(int i = ; i < n-; i++)
{
scanf("%d %d",&x,&y);
vec[x].push_back(y);
vec[y].push_back(x);
}
dfs();
memset(flag,,sizeof(flag));
dfs2();
int cn = ;
for(int i = ; i <= n; i++)
{
if(dp[i].cost1<=d)
{
cn++;
}
}
printf("%d\n",cn);
}
return ;
}
void Init()
{
for(int i = ; i < ; i++)
vec[i].clear();
memset(flag,,sizeof(flag));
for(int i = ; i <= ; i++)
{
dp[i].cost1 = ,dp[i].cost2 = ;
dp[i].id2 = -,dp[i].id1 = -;
if(ff[i])dp[i].id1 = i,dp[i].id2 = i;
}
}
void dfs(int n)
{
flag[n] = true;
for(int i = ; i < vec[n].size(); i++)
{
int id = vec[n][i];
if(!flag[id])
{
dfs(id);
if(dp[id].id1!=-)
{
if(dp[id].cost1+ > dp[n].cost1)
{
dp[n].cost2 = dp[n].cost1;
dp[n].id2 = dp[n].id1;
dp[n].cost1 = dp[id].cost1+;
dp[n].id1 = id;
}
else if(dp[id].cost1+ > dp[n].cost2)
{
dp[n].cost2 = dp[id].cost1+;
dp[n].id2 = id;
}
}
}
}
}
void dfs2(int n)
{
flag[n] = true;
for(int i = ; i < vec[n].size(); i++)
{
int id = vec[n][i];
if(!flag[id])
{
if(dp[n].id1!=-&&dp[n].id1 != id)
{
if(dp[n].cost1 + > dp[id].cost1)
{
dp[id].cost2 = dp[id].cost1;
dp[id].id2 = dp[id].id1;
dp[id].cost1 = dp[n].cost1+;
dp[id].id1 = n;
}
else if(dp[n].cost1 + > dp[id].cost2)
{
dp[id].cost2 = dp[n].cost1+;
dp[id].id2 = n;
}
}
else if(dp[n].id1!=-&&dp[n].id1 == id&&dp[n].id2!=-)
{
if(dp[n].cost2 + > dp[id].cost1)
{
dp[id].cost2 = dp[id].cost1;
dp[id].id2 = dp[id].id1;
dp[id].cost1 = dp[n].cost2+;
dp[id].id1 = n;
}
else if(dp[n].cost2 + >dp[id].cost2)
{
dp[id].cost2 = dp[n].cost2+;
dp[id].id2 = n;
}
}
dfs2(id);
}
}
}

D. Book of Evil的更多相关文章

  1. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  2. C#Light/Evil合体啦

    决定将C#Light和C#Evil合并成一个项目,毕竟C#Evil包含C#Light所有的功能,分开两个,基本的表达式方面有什么bug还得两头改 暂时就C#Light/Evil这么叫吧,庆祝合体,画了 ...

  3. C#最良心脚本语言C#Light/Evil,Xamarin\WP8\Unity热更新最良心方案,再次进化.

    C#Light的定位是嵌入式脚本语言,一段C#Light脚本是一个函数 C#Evil定位为书写项目的脚本语言,多脚本文件合作,可以完全用脚本承载项目. C#Light/Evil 使用完全C#一致性语法 ...

  4. Java unserialize serialized Object(AnnotationInvocationHandler、ysoserial) In readObject() LeadTo InvokerTransformer(Evil MethodName/Args)

    Java unserialize serialized Object(AnnotationInvocationHandler.ysoserial) In readObject() LeadTo Tra ...

  5. 只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs

    只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs [ide is evil] (http://i.cnblogs.com/EditP ...

  6. Gym 100463D Evil DFS

    Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...

  7. CF 337D Book of Evil 树形DP 好题

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

  8. Codeforces Gym 100463D Evil DFS

    Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...

  9. codeforces 337D 树形DP Book of Evil

    原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...

随机推荐

  1. 爬虫---request+++urllib

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...

  2. Linux下yum升级安装PHP 5.5

    我的系统是Centos 6.5 ,安装lnmp是直接yum安装的,php版本为5.4的,当安装了最新的phpMyAdmin(4.5.1)数据库管理软件后发现不支持php5.4使用,所以只好升级下php ...

  3. echo 换行不换行

    echo换行输出需要转义符 -e 看以下例子: echo -e "It is the first line." >> a; echo -e "It is th ...

  4. Odoo启动过程

    [本文基于odoo9源码编写] odoo包含的服务有 db object report workflow web[wsgi] Odoo以wsgi 规范提供Web及Web服务db/object/repo ...

  5. ios php RSA 非对称加密解密 der 和pem生成

    ios 使用public_key.der加密 php 使用 private_key.pem解密 openssl req -x509 -out public_key.der -outform der - ...

  6. Java+MySql图片数据保存与读取的具体实例

    1.创建表: drop table if exists photo;CREATE TABLE photo (    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ...

  7. Storm启动流程简介

    storm启动流程          storm是一个流行的开源的,分布式实时处理框架,关于storm的基本介绍可以参加这篇官方文档.大致的拓扑结构如图所示:        其中Nimbus是一个后台 ...

  8. 『TCP/IP详解——卷一:协议』读书笔记——08

    2013-08-21 13:56:23 3.3 IP路由选择 1. IP路由选择有两种情况.(1)如果目的主机与源主机直接相连(如点对点链路)或都在一个共享网络上(以太网或令牌环网),那么IP数据报就 ...

  9. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  10. MySql连接JDBC数据库------借鉴的红客联盟的

    首先安装MySQL数据库,我安装的是MySQL5.5,具体安装步骤这里就不介绍了.需要提醒的是,如果安装进程一直停在start service那里,无法继续进行下去的话,请参照我的博文<安装My ...