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是危险的. 现已知 ...
随机推荐
- java核心知识点学习----equals和==的比较、单例模式,饿汉式,饱汉式
最近发现自己学习能力变慢了,想来想去还是发现是因为自己Java基础没有打扎实,接下来的一系列文章将主要记录自己对于Java的最基础知识点的学习. 一.equals和==的比较 先看例子: packag ...
- iOS.ReactNative-2-bridge-and-react-native-app-execution
Bridge and React Native App Execution 基于0.18.1 Async batched bridge used to communicate with the Jav ...
- fail2ban 原理 安装 使用
cd fail2ban python setup.py install /etc/fail2ban/ 为配置文件目录; /usr/lib/pythonx.x/site-packages/fail2ba ...
- gvim 配置Pydiction
配置windows下gvim 的python关键字补全插件Pydiction 1.下载Pydiction 2.解压缩包,里面有after文件夹.complete-dict.pydiction.py 3 ...
- Android(Xamarin)之旅(一)
Xamarin废话我就不多说了. 就是一款编写Android和IOS应用的IDE,从Visual Studio2010就开始有个这个插件.只要发展什么的,我觉得在这里说还不如自己去百度呢. 入正题: ...
- flash网页播放器
http://www.52player.com/VideoPlayer/ 下载
- Base64编码通过URL传值的问题
base64 编码中使用了 +号,+号通过URL传递时会变成空格,因为编码的方式的问题前台使用:Ext.encode(title_text.getValue().replace(/\+/g, '%2B ...
- centos下 redis安装配置及简单测试
1:安装redis(使用的的环境是centos6.7 redis-2.6.14) 将redis-2.6.14.tar.gz文件拷贝到/usr/local/src 目录下 解压文件 tar zxvf ...
- Android之Fragment学习总结(1)
对于Fragment的学习: 近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效 ...
- mysql 全文搜索的FULLTEXT
FULLTEXT索引 创建FULLTEXT索引语法 创建table的时候创建fullText索引 CREATE TABLE table_name( column1 data_type, column2 ...