刷题总结——book of evil(codefoeces 337D)
题目:
description
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.
Input
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 birepresenting the ends of this path.
Output
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.
题解:
很妙的一道树形DP
我们用disd[u][1]表示在u所在子树中且距离节点u受影响的点的距离的最大值···disd[u][2]表示次大值··注意disd[u][1]与disd[u][2]保证两个值来自于两个不同的子树中(后面会知道为什么),disu[u]表示不在u所在子树中的影响点距u的距离的最大值····
disd[u][1]和disd[u][2]我们可以用一遍DFS求得···而disu我们需要在第二遍dfs时根据父节点来计算儿子节点的值···
首先设u的其中一个儿子为v,如果disd[v][1]+1==disd[u][1],那么disu[v]=max(disu[u]+1,disd[u][2]+1),否则disu[v]=max(disu[u]+1,disd[u][1]+1)
由此可以发现disd[u][2]有着给disu[u]的计算做铺垫的作用··三个值计算完后就可以判断了···
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
const int inf=0x3f3f3f3f;
int disd[N][],disu[N];
int first[N],next[N*],go[N*],tot,n,m,d;
bool jud[N];
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
return f;
}
inline void comb(int a,int b)
{
next[++tot]=first[a],first[a]=tot,go[tot]=b;
next[++tot]=first[b],first[b]=tot,go[tot]=a;
}
inline void dfs1(int u,int fa)
{
if(jud[u]) disd[u][]=disu[u]=;
for(int e=first[u];e;e=next[e])
{
int v=go[e];if(v==fa) continue;
dfs1(v,u);
if(disd[v][]+>disd[u][])
{
disd[u][]=disd[u][];disd[u][]=disd[v][]+;
}
else disd[u][]=max(disd[u][],disd[v][]+);
}
}
inline void dfs2(int u,int fa)
{
for(int e=first[u];e;e=next[e])
{
int v=go[e];if(v==fa) continue;
if(disd[u][]==disd[v][]+) disu[v]=max(disu[u]+,disd[u][]+);
else disu[v]=max(disu[u]+,disd[u][]+);
dfs2(v,u);
}
}
int main()
{
//freopen("a.in","r",stdin);
n=R(),m=R(),d=R();int a,b;
for(int i=;i<=n;i++) disd[i][]=disd[i][]=disu[i]=-inf;
for(int i=;i<=m;i++) a=R(),jud[a]=true;
for(int i=;i<n;i++) a=R(),b=R(),comb(a,b);
dfs1(,);dfs2(,);
int ans=;
for(int i=;i<=n;i++)
{
if(i==)
{
if(disd[i][]<=d&&disd[i][]<=d) ans++;
}
else if(disd[i][]<=d&&disu[i]<=d) ans++;
}
cout<<ans<<"\n";
return ;
}
刷题总结——book of evil(codefoeces 337D)的更多相关文章
- LeetCode刷题系列
LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...
- ife任务刷题总结(一)-css reset与清除浮动
本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...
- 刷题ING...
我用codeVS刷题.. 努力准备!!
- XidianOJ 1020 ACMer去刷题吧
题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...
- 【BZOJ-4590】自动刷题机 二分 + 判定
4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 156 Solved: 63[Submit][Status ...
- NOI题库分治算法刷题记录
今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...
- NOI题库刷题日志 (贪心篇题解)
这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制: 1000ms 内存限制: 65536kB 描述 在一个平面上,如果有两个点( ...
- 用js刷题的一些坑
leecode可以用js刷题了,我大js越来越被认可了是吧.但是刷题中会因为忽略js的一些特性掉入坑里.我这里总结一下我掉过的坑. 坑1:js中数组对象是引用对象 js中除了object还有数组对象也 ...
- BZOJ4590 自动刷题机
Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...
随机推荐
- CF Gym 100463D Evil (二维前缀和+离散)
题意:给一些带颜色的点,求一个最小的矩形,恰好包括一半的红色点,且不包括蓝色点. 题解:暴力,求个二维前缀和,用容斥原理更新一下.N很小所以我采用了离散优化,跑了个0ms. 之前没写过二维前缀和,加上 ...
- HDU 5097 Page Rank (模拟)
题目背景是以前用来对网页进行排名的Page Rank算法,是早期Google的革命性发明. 背后的原理是矩阵和图论.这个数学模型是由Google的创始人拉里·佩奇和谢尔盖·布林发现的. 如果一个网页被 ...
- oracle没有监听和监听程序无法找到适用于客户机连接的例程
1.无监听,可以尝试下以下几种办法: 1)在net manager中重新配置监听.我的net manager监听点开不了,把ADMIN下的listener.ora删掉再去打开试试. 2)cmd中输入n ...
- java基础——接口与抽象类的区别
(1)首先接口和抽象类的设计目的就是不一样的.接口是对动作的抽象,而抽象类是对根源的抽象. (2)对于抽象类,一个类只能继承一个抽象类.但是一个类可以同时实现多个接口. (3)接口是公开的,里面不能有 ...
- sessionStorage对象
sessStorage对象是一个会话形式的数据存储,当用户关闭浏览器的窗口后,数据就会被删除. 实例: <!DOCTYPE html><html><head> &l ...
- 分割catalina.out 每天生成一个文件
1. touch xxx(文件名字).sh 2. vim xxx.sh 写入 ----------------------- #!/bin/sh cd `dirname $0`pwdd=`d ...
- CentOS 7.4 基于LNMP搭建wordpress
之前有好多次搭建wordpress的经历,有在Ubuntu系统上,有在CentOS7.2系统上,但都是搭完还是稀里糊涂的,因为好多都是教程上照着敲的.这次好好出个教程,以便以后方便查看. 准备工作:C ...
- linux三剑客正则表达式
^:以...开头,^d,意思是以d开头.例如:ls -F(-p) | grep " ^d " $:以...结尾,/$,意思是以/结尾.例如:ls -F(-p) | grep &q ...
- Docker容器技术的核心原理
目录 1 前言 2 docker容器技术 2.1 隔离:Namespace 2.2 限制:Cgroup 2.3 rootfs 2.4 镜像分层 3 docker容器与虚拟机的对比 1 前言 上图是百度 ...
- poj 1328 安雷达问题 贪心算法
题意:雷达如何放置?在xoy二维平面坐标系里面,x轴上方的为岛屿,x轴下方的是雷达要放到位置,如何放使得雷达放的最少? 思路 肯定放在x轴上减少浪费是最好的选择 什么情况下,雷达无法到达呢?--以这个 ...