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内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
随机推荐
- 树状dp ural1018
#include<stdio.h> #include<string.h> #include <iostream> using namespace std; ; in ...
- 洛谷 P1896 互不侵犯King
P1896 [SCOI2005]互不侵犯King 题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共 ...
- ORA-01722: invalid number,ORA-12801
SQL: SELECT /*+ parallel(a,32) */ a.id ,a.data_date ,a.mobile_num ,a.mobile_code ,b.prov AS mobile_p ...
- 克隆contos 出现 Error:No suitable device found: no device found for connection &quot;System eth0&
二.问题 这时我复制好的虚拟机,启动登陆进去(用户名和密码跟之前那台是一样的),修改好IPADDR,然后网卡重启出现问题? #service network restart 出现问题:Error:No ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- Jasper_table_pass parameter to table component
<subDataset name="Dataset1" uuid="2a894ef4-dbcc-47df-bfaf-027766c7352e"> 2 ...
- My Eclipse 自动提示
1.My Eclipse 自带代码提示快捷键 “ alt+/”. 2.输入即提示:window-->preferences-->java-->Editor 展开后点击Content ...
- 类XX是公共的,应在名为XX.java的文件中声明public class XX
找了一个程序粘贴到txt文档中,随便起个名为abc,然后改后缀为.java,接下来在dos中运行时出现以上错误 打开abc.java看了看,声明public class了,但是名字是粘贴过来的类名Wo ...
- openFileDialog与saveFileDialog的使用
private void btnOpen_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogR ...
- DDP和DDU什么区别
DU/DDP 就是A发货给国外B,B只要呆在家里看电视,货会自动送上门,当中的所有运输清关等事情都是由A来负责(A可以委托货代来负责),区别就是DDU是不包括税金的,也就是货值的百分之多少,税金会在B ...