转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Book of Evil

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.

Sample test(s)
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.

题意

给一棵n个结点的树,在树上的某个点上有一本"book of evil",在其周围的与其距离小于等于d的点都会受到其影响,已知m个受到影响的点,求有几个可能会有"book of evil"

通过维护一个结点到其子树中的最长距离,以及除去存在最长距离之外,到另外子树的最长距离,然后再dfs一遍即可。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 100010
vector<int>G[MAXN];
int dp[MAXN][];
int pre[MAXN][];
int ans=;
int d;
void dfs(int u,int fa){
for(int i=;i<G[u].size();i++){
int v=G[u][i];
if(v==fa)continue;
dfs(v,u);
int d1=dp[v][]+;
if(d1>dp[u][]){
dp[u][]=d1;
pre[u][]=v;
if(dp[u][]>dp[u][]){
swap(dp[u][],dp[u][]);
swap(pre[u][],pre[u][]);
}
}
}
}
void dfs2(int u,int fa,int dis){
if(dis>d)return;
if(dp[u][]<=d)ans++;//cout<<u<<endl;}
int d1=max(dp[u][],dis)+;
int d2=max(dp[u][],dis)+;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(v==fa)continue;
if(pre[u][]==v)dfs2(v,u,d2);
else dfs2(v,u,d1);
}
}
int main()
{
ios::sync_with_stdio(false);
//freopen("in.in","r",stdin);
int m,n;
int u,v;
cin>>n>>m>>d;
for(int i=;i<n;i++)dp[i][]=dp[i][]=-MAXN;
for(int i=;i<n;i++)pre[i][]=pre[i][]=-;
for(int i=;i<m;i++){
cin>>u;
u--;
dp[u][]=dp[u][]=;
}
for(int i=;i<n;i++)G[i].clear();
for(int i=;i<n-;i++){
cin>>u>>v;
u--;v--;
G[u].push_back(v);
G[v].push_back(u);
}
ans=;
dfs(,-);
dfs2(,-,-MAXN);
cout<<ans<<endl;
return ;
}

代码君

codeforces 337D Book of Evil(dp)的更多相关文章

  1. codeforces 337D Book of Evil (树形dp)

    题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...

  2. Codeforces 337D Book of evil

    一道树形dp,写出来是因为最近也做了道类似的.这题是看了分析的思路才做出来的,但感觉很多这样的dp都是利用类似的性质.像这题的话distDown很好想,但distUp的时候就很难想了,其实只要抓住di ...

  3. Codeforces 337D Book of Evil:树的直径【结论】

    题目链接:http://codeforces.com/problemset/problem/337/D 题意: 给你一棵树,n个节点. 如果一个节点处放着“罪恶之书”,那么它会影响周围距离不超过d的所 ...

  4. codeforces 337D 树形DP Book of Evil

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

  5. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  6. 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 ...

  7. codeforce 337D Book of Evil ----树形DP&bfs&树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  8. CodeForces - 337D 树形dp

    题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...

  9. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

随机推荐

  1. OpenSuse13.2硬盘安装

    直接参考文章:OpenSuse硬盘安装 补充: Win7引导Grub4dos时,本人尝试根据xp引导方式中使用boot.ini来引导,引导成功,不需要bcdedit命令,简化了引导步骤.

  2. 关于Windows8.1更新后Sql Server服务消失的处理办法

    前言 微软在17日发布了windows8.1,兴致勃勃地花了半天的时间更新了,不过不知所云的是,在20日又被卸下Windows Store.此为背景. 影响 更新完毕做开发的时候,发现SqlServe ...

  3. 字典:当索引不好用时2 - 零基础入门学习Python026

    字典:当索引不好用时2 让编程改变世界 Change the world by program 上节课我们学习到在一些情况下,比序列更实用的映射类型:字典.我们知道字典也有个关键符号就是大括号(也叫花 ...

  4. Effective Java2读书笔记-类和接口(五)

    第21条:用函数对象表示策略 这一条其实也没说啥,就是策略模式.碰到这种场景时,定义一个策略接口,然后不同策略子类实现它,主类包含这个接口的引用就可以了. 第22条:优先考虑静态成员类 嵌套类是指被定 ...

  5. 使用URL创建网络连接、网络流的阻塞问题

    在读取网络中流数据时,通常要创建一个网络连接.然而在创建URL连接时,我们通常会忽略掉设置ConnectTimeout,以及ReadTimeout: URL url = new URL(urlstr) ...

  6. 使用WCF实现SOA面向服务编程—— 架构设计

    原文地址:http://www.cnblogs.com/leslies2/archive/2011/03/29/1997889.html SOA本身就是一种面向企业级服务的系统架构,简单来说,SOA就 ...

  7. Android Studio 配置SVN实现代码管理

    Refference From:http://iaiai.iteye.com/blog/2267346 一.Android Studio配置SVN Android Studio关联配置SVN很简单,在 ...

  8. Nim Game 解答

    Question You are playing the following Nim Game with your friend: There is a heap of stones on the t ...

  9. Hdu2860-Regroup(种类并查集)

    Problem Description When ALPC42 got to a panzer brigade, He was asked to build software to help them ...

  10. HDU4171--bfs+树

    第一开始想成了DP.尼玛后来才发现只有N条边,那就简单了.. 从起点S遍历整棵树,从某点跳出来回到终点T,问最短路长度.然而从某点跳出时走过的路径是一个定值.... 长度为整棵树的边长和sum*2-d ...