codeforces 337D Book of Evil(dp)
转载请注明出处: 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.
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.

题意
给一棵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)的更多相关文章
- codeforces 337D Book of Evil (树形dp)
题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...
- Codeforces 337D Book of evil
一道树形dp,写出来是因为最近也做了道类似的.这题是看了分析的思路才做出来的,但感觉很多这样的dp都是利用类似的性质.像这题的话distDown很好想,但distUp的时候就很难想了,其实只要抓住di ...
- Codeforces 337D Book of Evil:树的直径【结论】
题目链接:http://codeforces.com/problemset/problem/337/D 题意: 给你一棵树,n个节点. 如果一个节点处放着“罪恶之书”,那么它会影响周围距离不超过d的所 ...
- codeforces 337D 树形DP Book of Evil
原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- 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 ...
- codeforce 337D Book of Evil ----树形DP&bfs&树的直径
比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...
- CodeForces - 337D 树形dp
题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
随机推荐
- xml解析,练习
<collection shelf="New Arrivals"><movie title="Enemy Behind"> < ...
- python学习第一天 -安装配置及其输入输出
Python, 是一种面向对象.解释型计算机程序设计语言. python适合领域: 1.Web网络和各种网络服务 2.系统工具和脚本 3.作为“胶水”语言把其他语言开发的模块包装起来方便使用 pyth ...
- jquery实现导航栏头部点击变换颜色
实现效果如下: 话不多说直接上代码: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- 【Android】使用Gson和Post请求和服务器通信
一.需求文档如下: URL:http://108.188.129.56:8080/example/cal 请求格式: {"para1":10,"para2":2 ...
- 转:Reddit排名算法工作原理
http://www.aqee.net/how-reddit-ranking-algorithms-work/ 这是一篇继<Hacker News 排名算法工作原理>之后的又一篇关于排名算 ...
- C#代码计时
using System.Diagnostics; Stopwatch sw = new Stopwatch(); sw.Start(); //todo code ....... sw.Stop(); ...
- codility上的问题 (21) Upsilon 2012
这是我目前最喜欢的codiltiy上的问题之一.问题描述是:给定一个整数数组A,所有的数均不相同.假设下标从0开始,找到一个数组B, 满足A[B[0]] > A[B[1]] > A[B[2 ...
- LCS算法思想
LCS问题就是求两个字符串最长公共子串的问题.解法就是用一个矩阵来记录两个字符串中所有位置的两个字符之间的匹配情况,若是匹配则为1,否则为0.然后求出对角线最长的1序列,其对应的位置就是最长匹配子串的 ...
- WPF Image触摸移动方法
1: TouchPoint mPoint = null; 2: double mOffsetX;//水平滚动条当前位置 3: double mOffsetY;//垂直滚动条当前位置 4: bool m ...
- Quartus DSE 初步应用
介绍 Design Space Explorer (DSE) is a program that automates the process of finding the optimal collec ...