D. Book of Evil
2 seconds
256 megabytes
standard input
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.
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.
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.
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
3
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的更多相关文章
- 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 ...
- C#Light/Evil合体啦
决定将C#Light和C#Evil合并成一个项目,毕竟C#Evil包含C#Light所有的功能,分开两个,基本的表达式方面有什么bug还得两头改 暂时就C#Light/Evil这么叫吧,庆祝合体,画了 ...
- C#最良心脚本语言C#Light/Evil,Xamarin\WP8\Unity热更新最良心方案,再次进化.
C#Light的定位是嵌入式脚本语言,一段C#Light脚本是一个函数 C#Evil定位为书写项目的脚本语言,多脚本文件合作,可以完全用脚本承载项目. C#Light/Evil 使用完全C#一致性语法 ...
- Java unserialize serialized Object(AnnotationInvocationHandler、ysoserial) In readObject() LeadTo InvokerTransformer(Evil MethodName/Args)
Java unserialize serialized Object(AnnotationInvocationHandler.ysoserial) In readObject() LeadTo Tra ...
- 只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs
只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs [ide is evil] (http://i.cnblogs.com/EditP ...
- Gym 100463D Evil DFS
Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...
- 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 ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...
- codeforces 337D 树形DP Book of Evil
原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...
随机推荐
- 【BZOJ1226】学校食堂Dining(状压DP)
题意:见题面 思路:设dp[i,sta,k]为前i个人已经吃完,从第i人到第i+b[i]人的吃饭状况是sta,前一个吃完的人离i的距离是k(可能为负)的最小值 \[ dp[i+1,sta>> ...
- ASP.NET ZERO 学习 事件总线
用于注册和触发客户端的全局事件. 介绍 Pub/sub事件模型广泛用于客户端,ABP包含了一个简单的全局事件总线来 注册并 触发事件. 注册事件 可以使用abp.event.on来注册一个全局事件.一 ...
- iOS 如何给Xcode7项目添加“.pch”文件
1.首先打开你的项目(演示使用一个空的项目),按照以下步骤即可 找到“Supporting Files”文件夹,右键即可看到下图,选择“New File...” 2.选择"iOS" ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- jquery中prop()与attr()方法的区别
一.prop() 简单来说是当需要判断真假时使用,如复选框时: if( $(this).prop('checked')){ //当返回true时在这里调用 }else{ //当返回false时在这里调 ...
- dede channelartlist 中引用channel 并且设置当前选择类的样式,currentstyle暂时没有效果.特发求助!!!!
在dede中需要达到当前选择了该类,样式不同的话.如果是channelartlist 的话. 可以使用下面的情况: {dede:channelartlist typeid='6' row='3' cu ...
- 使用PreTranslateMessage替代钩子函数处理键盘消息
2002年左右,我所在公司在开发基于H.323的VoIP电话系统(用了以色列一家公司的库,具体名字忘记了). 去电信科技研究院测试系统,同事发现处理键盘消息总有一些莫名其妙的问题,比如延迟或异常. 我 ...
- three.js 相关概念
1.什么是three.js? Three.js 是一个 3D JavaScript 库.Three.js 封装了底层的图形接口,使得程序员能够在无需掌握繁冗的图形学知识的情况下,也能用简单的代码实现三 ...
- eclipse中无法使用fat.jar
因为某种需要,我要打jar包,而eclipse中自带的打包功能又太过于繁琐,因此找到这个插件.不过尝试了许久都没有成功,最后终于找到了原因,是因为该插件的版本太低的缘故.相见:https://code ...
- 自定义UISearchBar外观
1. 设置背景色 我以白色的背景色为例,下面看看代码: //1. 设置背景颜色 //设置背景图是为了去掉上下黑线 self.customSearchBar.backgroundImage = [[UI ...