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是危险的. 现已知 ...
随机推荐
- phpcms安装
cms的样式有很多种,我们学习的是phpcms,这些cms都是大同小异,学会了一种就可以使用其它的cms. PHPCMS是一款网站管理软件.该软件采用模块化开发,支持多种分类方式,使用它可方便实现个性 ...
- Convert HTML Entities
function convert(str) { // :) //return str; var HTML_Entities = { '&':'&', '<':'<', '& ...
- redis 内存泄露
http://www.oschina.net/question/2266476_246221 http://stackoverflow.com/questions/24304212/how-to-de ...
- 让IE8支持HTML5及canvas功能!chart.js图表绘制工具库IE8上兼容方案
第一步,我们加上对html5的支持. <!--[if IE]> <script src="/public/html5.js" type="text/ja ...
- BigDecimal类的加减乘除
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算. 双精度浮点型变量double可以处理16位有效数,但是在实际应用中,需要对更大或者更小的 ...
- 第二章 ZAB协议介绍
ZAB ( ZooKeeper Atomic Broadcast , ZooKeeper 原子消息广播协议)是zookeeper数据一致性的核心算法. ZAB 协议并不像 Paxos 算法那样,是一种 ...
- 咏南WEB开发框架(FOR XE10.1 BERLIN)
咏南WEB开发框架(FOR XE10.1 BERLIN) 1)支持最新的XE10.1 BERLIN开发WEB程序 2)如同开发VCL WIN32程序一样的速度 3)WEB框架通过咏南中间件和数据库打交 ...
- Infobright存储引擎的特点
Infobright的优点: (1)高压缩比率 (2)快速响应复杂的分析查询语句 (3)随着数据库的逐渐增大,查询和装载性能基本保持稳定 (4)没有特殊的数据仓库模型(比如星状模型.雪花模型)要求 ( ...
- PoEdu - C++阶段班- Lesson07 To Lesson10_C to C++
07 重载导致的二义性 问题:为什么一定要重载呢?重载能方便我们注重函数的功能,当参数类型不确定时,我们能很便捷的利用重载的机制达到目的. 重载注意点:二义性 看代码: #include <c ...
- STF(SmartPhone Test Farm)Mac版本环境搭建
它的github页面为: https://github.com/openstf/stf 1.Linux一些基本包的安装: 在控制台分别运行 sudo apt-get update sudo apt-g ...