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内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
随机推荐
- hdu5672 尺取法
StringTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- CODEVS 3279 奶牛的健美操
3279 奶牛健美操 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description Farmer John为了保持奶牛们的 ...
- Jquery去除从数据库中查询到的内容含有的p标签
$("#topic_content").html($("#topic_content").text()); 如果这个数据是通过循环遍历出的数据,就需要下面这个代 ...
- C#设置按钮三态背景图片
1.闪电事件,注意添加手动或者点击按钮->属性中的闪电->下面对应各种事件 在上面: private void button1_MouseHover(object sender, Even ...
- 移动端Web开发笔记
最近写的一个移动端项目:上海 地铁指路通,之间遇到的一些问题,记录下来(以后会不断补充的): 1. 丰富的页面Meta: 1.1: 控制显示区域各种属性: <meta content=" ...
- django解决跨域请求的问题
跨域请求可以用jsonp来解决,不过今天我发现一个很好用的包:django-cors-headers 只需要简单地配置一下就可 被请求方的setting.py中的配置如下: INSTALLED_APP ...
- DataTables选择多行
$(document).ready(function() { var table = $('#example').DataTable(); $('#example tbody').on( 'click ...
- a标签href不跳转 禁止跳转
a标签href不跳转 禁止跳转 当页面中a标签不需要任何跳转时,从原理上来讲,可分如下两种方法: 标签属性href,使其指向空或不返回任何内容.如: <a href="javascri ...
- C语言实现OOP 版本2
写版本2的原因,还是发现在不同的具体图形模块里发现了重复的release代码,这是坏味道,所以还是决定消除这些重复代码,DRY! shape.h #ifndef SHAPE_H #define SHA ...
- Unix,windows和Mac中的换行
Unix 系统里,每行结尾只有“<换行>”,即“\n”:Windows系统里面,每行结尾是“<换行><回车 >”,即“\r\n”:Mac系统里,每行结尾是“< ...