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 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.
题意:
1棵树,n个节点,编号为1~n,树的边权都是1再
给出m,d,然后有m个数
已知在某一个节点上有一个武器,与这个武器距离在d以内的节点都会受到辐射
现在已经知道有m个节点受到了辐射,问武器可能在的节点的个数
即求:这棵树上到这m个节点的距离都<=d的节点的个数。
树形DP,开始不知道怎么DP,总想着暴力。
令tree(i)表示以节点i为根的子树
把这m个节点称之为辐射点
dp[i][1] 表示tree(i)中,与i距离最远的辐射点的距离
dp[i][2] 表示tree(i)中,与i距离第二远的辐射点的距离(求dp[i][0]的时候需要用到)
dp[i][0] 表示整棵树-tree(i)中,与i距离最远的辐射点的距离
则与i距离最远的辐射点的距离=max(dp[i][1],dp[i][0])
若max(dp[i][0],dp[i][1])<=d,则节点i可能是武器的位置
则要求的就是满足max(dp[i][1],dp[i][0])<=d的i的个数
siz[i] 表示tree(i)中,辐射点的个数
son[i] 表示tree(i)中,dp[i][1]经过i的儿子节点son[i]
use[i] 表示节点i是不是辐射点
3次dfs,分别求出dp[i][1],dp[i][2],dp[i][0]
统计个数
#include<cstdio>
#include<cstring> using namespace std; const int maxn=1e5+;
const int inf=0x3f3f3f3f; inline int max(int a,int b)
{
return a>b?a:b;
} int dp[maxn][];
int siz[maxn];
bool use[maxn];
int son[maxn];
struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot=; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void solve(int ,int ,int );
void dfs0(int ,int );
void dfs1(int ,int );
void dfs2(int ,int ,int ); int main()
{
memset(head,-,sizeof head);
memset(use,false,sizeof use);
memset(son,-,sizeof son);
int n,m,d;
scanf("%d %d %d",&n,&m,&d);
for(int i=;i<=m;i++){
int u;
scanf("%d",&u);
use[u]=true;
} for(int i=;i<n;i++){
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n,m,d);
return ;
} void solve(int n,int m,int d)
{
memset(dp,,sizeof dp);
dfs0(,-);
dfs1(,-);
dfs2(,-,m); int ans=;
for(int i=;i<=n;i++){
if(max(dp[i][],dp[i][])<=d)
ans++;
}
printf("%d\n",ans);
return ;
} void dfs0(int u,int pre)
{
if(use[u])
siz[u]=;
else
siz[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
dfs0(v,u);
if(siz[v]){
dp[u][]=max(dp[u][],dp[v][]+);
siz[u]+=siz[v];
if(son[u]==-||dp[v][]>dp[son[u]][])
son[u]=v;
}
}
} void dfs1(int u,int pre)
{
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
if(siz[v]){
dfs1(v,u);
if(v==son[u]){
continue;
}
else{
dp[u][]=max(dp[u][],dp[v][]+);
}
}
}
} void dfs2(int u,int pre,int m)
{
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
if(m>siz[v]){
if(v==son[u])
dp[v][]=max(dp[u][],dp[u][])+;
else
dp[v][]=max(dp[u][],dp[u][])+;
}
dfs2(v,u,m);
}
}
CF 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 题目大 ...
- codeforce 337D Book of Evil ----树形DP&bfs&树的直径
比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...
- POJ 1155 TELE 背包型树形DP 经典题
由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...
- POJ 2342 树形DP入门题
有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...
- 51nod 1353 树 | 树形DP经典题!
51nod 1353 树 | 树形DP好题! 题面 切断一棵树的任意条边,这棵树会变成一棵森林. 现要求森林中每棵树的节点个数不小于k,求有多少种切法. 数据范围:\(n \le 2000\). 题解 ...
- P2016 战略游戏——树形DP大水题
P2016 战略游戏 树形DP 入门题吧(现在怎么是蓝色标签搞不懂): 注意是看见每一条边而不是每一个点(因为这里错了好几次): #include<cstdio> #include< ...
- (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520
题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...
- CF EDU 1101D GCD Counting 树形DP + 质因子分解
CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...
- CF 461B Appleman and Tree 树形DP
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
随机推荐
- spark中streamingContext的使用详解
两种创建方式 val conf = new SparkConf().setAppName(appName).setMaster(master);val ssc = new StreamingConte ...
- windows日常软件推荐
下面的软件都是本人实际使用过的. 我只是推荐,没逼着你用,也没收谁的钱做广告. 操作系统win7 64bits. 不定期更新. [QQ轻聊版] 本人就是一个码畜,上班族,天气好坏都得挤地铁去上班,也没 ...
- ps互补色
色彩中的互补色有红色与绿色互补,蓝色与橙色互补,紫色与黄色互补.在光学中指两种色光以适当的比例混合而能产生白光时,则这两种颜色就称为“互为补色”. 互补色是相对的混合的白色 互补色:在色环中某种颜色的 ...
- C#脱离Halcon编程开发环境使用方法
在没有安装Halcon开发程序(HDevelop (SSE2))的电脑上面编程,使C#脱离Halcon编程开发环境使用方法,除了按照Halcon与编程环境必须要做的设置步骤外,还需要做如下两个工作: ...
- 【Unity3D基础教程】给初学者看的Unity教程(六):理解Unity的新GUI系统(UGUI)
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! 理解UGUI的基础架构 UGUI是Unity在4 ...
- share point 2013 显示详细错误信息?
Wednesday, April 6, 2011 at 17:40 | Post a Comment SharePoint "Unknown Error": How to Sho ...
- browser-ua
故事还得从头说起,最初的主角叫NCSA Mosaic,简称Mosaic(马赛克),是1992年末位于伊利诺伊大学厄巴纳-香槟分校的国家超级计算机应用中心(National Center for Sup ...
- 【转】WMI使用的WIN32_类库名
ShadowBy--Win32_ShadowContext--Win32_ShadowCopy--Win32_ShadowDiffVolumeSupport--Win32_ShadowFor--Win ...
- Knockout.js, Asp.Net MVC and Bootstrap 前端设计
原文地址:http://ddmvc4.codeplex.com/ 原文名称:Design and Develop a website using ASP.NET MVC 4, EF, Knockout ...
- JQuery Pagenation 知识点整理——arguments,callee,caller,apply应用(20150517)(转)
arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[n]参数function :选项.当前正在执行的 Function 对象的名字. n :选 ...