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是危险的. 现已知 ...
随机推荐
- sendEmail的使用
坑!坑!坑!今天又弄了一天如何发送邮件,首先用sendmail,postfix.....等,都试过了,各种报错,然后就从早上弄到现在,在群里问问题的时候,一位好心的大哥说了他用的是sendEmail, ...
- 附12 grafana配置文件
一.配置文件位置 /usr/local/opt/grafana/share/grafana/default.ini /usr/local/etc/grafana/grafana.ini 二.init格 ...
- 附10 kibana创建新的index patterns
elk整体架构图: 一.logstash indexer 配置文件: input { stdin{} } filter { } output { elasticsearch { hosts => ...
- 小甲鱼python视频第八讲(课后习题)
1.for循环的使用,注意下面代码的区别 for i in range(0,10,2): print("i love you") for i in range(0,10): pri ...
- sd_cms置顶新闻,背景颜色突击显示
维护之前的一个客户网站,使用的是sd_cms系统,因为好久没有维护了,看到这网站的时候,真不敢相信,自己也曾做出过这样的网站. 客户要求置顶新闻始终在最上面,有背景颜色突击显示. 找到对应的代码,修改 ...
- Sql 中text类型字段判断是否为空
用 len关键字,字段=''会报错:数据类型 text 和 varchar 在 equal to 运算符中不兼容. 正确方法: 1. 字段 is null 2. datalength(字段)=0 注: ...
- php 递归函数的三种实现方式
递归函数是我们常用到的一类函数,最基本的特点是函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去.实现递归函数可以采取什么方式呢?本文列出了三种基本方式.理解其原来需要一定的基础知识 ...
- HtDP读后感
我小时候很喜欢电脑. 不是听音乐,也不是玩游戏.纯粹是好奇:为什么一部冷冰冰的机器插上电源之后居然能如此强大.我每个月都期盼着订阅的电脑杂志快点到,到手以后总先上手试试里面的"奇淫巧技&qu ...
- 《深入理解Spark:核心思想与源码分析》正式出版上市
自己牺牲了7个月的周末和下班空闲时间,通过研究Spark源码和原理,总结整理的<深入理解Spark:核心思想与源码分析>一书现在已经正式出版上市,目前亚马逊.京东.当当.天猫等网站均有销售 ...
- 【Map】MapTest
package cn.itcast.p1.map.test; import java.util.HashMap; import java.util.Map; public class MapTest2 ...