题目:

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 nm 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)的更多相关文章

  1. LeetCode刷题系列

    LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...

  2. ife任务刷题总结(一)-css reset与清除浮动

    本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...

  3. 刷题ING...

    我用codeVS刷题.. 努力准备!!

  4. XidianOJ 1020 ACMer去刷题吧

    题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...

  5. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

  6. NOI题库分治算法刷题记录

    今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...

  7. NOI题库刷题日志 (贪心篇题解)

    这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制:  1000ms  内存限制:  65536kB 描述 在一个平面上,如果有两个点( ...

  8. 用js刷题的一些坑

    leecode可以用js刷题了,我大js越来越被认可了是吧.但是刷题中会因为忽略js的一些特性掉入坑里.我这里总结一下我掉过的坑. 坑1:js中数组对象是引用对象 js中除了object还有数组对象也 ...

  9. BZOJ4590 自动刷题机

    Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...

随机推荐

  1. noip模拟赛#45

    T1:n<=1e6,求最小的区间包含(1,m)的所有数. =>双指针扫一遍即可 #include<cstdio> #include<cstring> #includ ...

  2. [].indexOf.call()学习

    今天看到闭包一道题,就是一个li列表,点击列表控制台输出对应的索引.这里考察了var的作用域问题和闭包对外部变量的引用问题,有几种解决方法. html: <ul> <li>te ...

  3. Seek and Destroy-freecodecamp算法题目

    Seek and Destroy(摧毁数组) 要求 实现一个摧毁(destroyer)函数,第一个参数是待摧毁的数组,其余的参数是待摧毁的值. 思路 利用for循环将输入arguments中除第一项待 ...

  4. [转载] bp神经网络推导

    https://blog.csdn.net/fanxin_i/article/details/80212906 看了这么多就这个推的清楚,转嘞

  5. 【贪心】10.24assassin

    题目分析 没有题目分析…… 寄存一下神奇反悔贪心 #include<bits/stdc++.h> ; struct node { int a,b; node(, ):a(x),b(y) { ...

  6. k8s调度的预选策略及优选函数

    scheduler调度过程:    Predicate(预选)-->Priority(优选)-->Select(选定)调度方式:    1.节点亲和性调度(NodeAffinity)使用n ...

  7. 剑指Offer(书):顺时针打印数组

    题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1 ...

  8. ax=1(%b) 求最小逆元

    定理一:如果d = gcd(a, b),则必能找到正的或负的整数x和y,使 d = a*x+ b*y. 定理二:若gcd(a, b) = ,则方程ax ≡ c (mod b)在[, b-]上有唯一解. ...

  9. 使用powershell/vbs自动化模拟鼠标点击操作

    今天想做windows上的自动化,所以才有了模拟鼠标点击的需求,先考虑用powershell实现: 首先先安装一个名为“WASP”免费可用的Powershell扩展程序,下载地址:http://was ...

  10. HDU 5536 Chip Factory Trie

    题意: 给出\(n(3 \leq n \leq 1000)\)个数字,求\(max(s_i+s_j) \bigoplus s_k\),而且\(i,j,k\)互不相等. 分析: 把每个数字看成一个\(0 ...