Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接:
http://codeforces.com/problemset/problem/337/D
D. Book of Evil
time limit per test2 secondsmemory limit per test256 megabytes
#### 问题描述
> 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
题意
给你一颗树,边长都为1,魔鬼会在某个点释放魔法,魔法的影响范围是d现在告诉你,若干个被影响的城市,叫你求魔鬼可能在的顶点数量。
题解
树dp维护一个点到最远的被影响城市的距离。类似直径一样转移。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=101010;
VI G[maxn];
int n,m,d;
int dp[maxn][2],id[maxn];
bool arr[maxn];
///dp[u][0]:最大值,dp[u][1]:次大值
void dfs(int u,int fa){
dp[u][0]=dp[u][1]=-INF;
if(arr[u]) dp[u][0]=dp[u][1]=0;
id[u]=-1;
rep(i,0,G[u].sz()){
int v=G[u][i];
if(v==fa) continue;
dfs(v,u);
if(dp[v][0]<0) continue;
if(dp[u][0]<dp[v][0]+1){
dp[u][1]=dp[u][0];
dp[u][0]=dp[v][0]+1;
id[u]=v;
}else if(dp[u][1]<dp[v][0]+1){
dp[u][1]=dp[v][0]+1;
}
}
}
void dfs2(int u,int fa,int ma){
dp[u][0]=max(dp[u][0],ma);
rep(i,0,G[u].sz()){
int v=G[u][i];
if(v==fa) continue;
if(id[u]==v){
dfs2(v,u,max(dp[u][1],ma)+1);
}else{
dfs2(v,u,max(dp[u][0],ma)+1);
}
}
}
int main() {
clr(arr,0);
scf("%d%d%d",&n,&m,&d);
rep(i,0,m){
int x; scf("%d",&x);
arr[x]=1;
}
rep(i,0,n-1){
int u,v;
scf("%d%d",&u,&v);
G[u].pb(v);
G[v].pb(u);
}
dfs(1,-1);
dfs2(1,-1,-INF);
int cnt=0;
for(int i=1;i<=n;i++) if(dp[i][0]<=d) cnt++;
prf("%d\n",cnt);
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp的更多相关文章
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP
C. Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some g ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #196 (Div. 1) 题解
(CF唯一不好的地方就是时差……不过还好没去考,考的话就等着滚回Div. 2了……) A - Quiz 裸的贪心,不过要用矩阵乘法优化或者直接推通式然后快速幂.不过本傻叉做的时候脑子一片混乱,导致WA ...
- Codeforces Round #196 (Div. 2) B. Routine Problem
screen 尺寸为a:b video 尺寸为 c:d 如果a == c 则 面积比为 cd/ab=ad/cb (ad < cb) 如果b == d 则 面积比为 cd/ab=cb/ad (c ...
- Codeforces Round #196 (Div. 2)
A 题意:O(-1) 思路:排个序搞定. B 题意:O(-1) 思路:坑了我好久,这个框框水平垂直比例固定,分两种情况即可,不能旋转,我想多了,分了四种情况. C 题意:一列n个位置,让你填m个数,当 ...
- A. Puzzles CodeForces Round #196 (Div.2)
题目的大意是,给你 m 个数字,让你从中选 n 个,使得选出的数字的极差最小. 好吧,超级大水题.因为要极差最小,所以当然想到要排个序咯,然后去连续的 n 个数字,因为数据不大,所以排完序之后直接暴力 ...
- Codeforces Round #196 (Div. 2) A. Puzzles 水题
A. Puzzles Time Limit: 2 Sec Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...
随机推荐
- 阿里云linux服务器打开端口号
之前linux回滚了下,然后就连不上xshell和filezille了,后台安全配置哪里也都打开了端口号了,还是不行.然后我就想重启下ssh服务 ,执行service sshd restart 提示1 ...
- uniapp开发踩坑记录
数组绑定class的问题 版本:v1.5.4 自定义了一个icon的组件,部分代码如下 <template> <text :class="[name, icon]" ...
- 关于 class helper for ... 语法
转载于万一的博客https://www.cnblogs.com/del/archive/2009/10/13/1582789.html class helper 可能是从 Delphi 2007 增加 ...
- 【树形DP】洛谷P1352_没有上司的舞会
本人第一篇Blog,初学树形DP,心情别样鸡冻... 好了废话不多说,我们来看看题目[传送门] 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是 ...
- 20155207王雪纯 Exp2 后门原理与实践
20155207王雪纯 Exp2 后门原理与实践 实验步骤 一.windows获取Linux shell Windows:使用 ipconfig 命令查看当前机器IP地址. 进入ncat所在文件地址, ...
- CF 1107 E. Vasya and Binary String
E. Vasya and Binary String 链接 分析: 对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可. 然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为 ...
- Keepalived高可用集群
一.服务介绍 keepalive起初是专为LVS设计的,专门用来监控LVS集群系统红各个服务节点的状态,后来又加入了VRRP的功能,因此不了配合LVS服务外,也可以作为其他服务(nginx,hapro ...
- 扩展gcd算法
扩展gcd算法 神tm ×度搜索exgcd 打到exg的时候出来ex咖喱棒... 球方程\(ax+by=\gcd(a,b)\)的一个解 如果\(b=0\),那么\(\gcd(a,b)=a\),取\(x ...
- 【JUC源码解析】ForkJoinPool
简介 ForkJoin 框架,另一种风格的线程池(相比于ThreadPoolExecutor),采用分治算法,工作密取策略,极大地提高了并行性.对于那种大任务分割小任务的场景(分治)尤其有用. 框架图 ...
- Windows7 jmeter3.1安装(咋个安装?)
这是一个比较详细的安装教程0.0,不懂可以私我,有错也可以私我 1.0 首先我们得有jdk,我选择的是1.8版本的jdk, QQ群:550654190,(进群答案:李熠)群文件里有. 进去后下载 ...