题目:

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. CoreData介绍

    http://blog.csdn.net/zh952016281/article/details/52105683 写在前面 在CoreData中有一些常用的类,称呼可能各不相同.所以这里先约定一些关 ...

  2. CPP-STL:vector中的size和capacity

    在vector中与size()和capacity() 相对应的有两个函数: resize(size_type)和reserve(size_type). Size指目前容器中实际有多少元素,对应的res ...

  3. CPP-基础:new int[]跟int()的区别

    1. new int[] 是创建一个int型数组,数组大小是在[]中指定,例如: int * p = new int[10]; //p执行一个长度为10的int数组.2. new int()是创建一个 ...

  4. C++ Stack 与String

    // ConsoleApplication1.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h" ...

  5. atoi 函数实现

      要考虑的东西实在也挺多的.总结如下:   1 前面空格分隔符号的时候   2 第一个符号位处理+ -   3 遇到非数字字符退出   4 为正数的时候,大于INT_MAX上溢   5 为负数的时候 ...

  6. unity3d sqlite数据库的读写方法

    首先,我们要从unity的安装路径中复制mono.data.sqlite.dll和sqlite3.dll两个动态链接库到untiy的plugins目录下,如下图所示: 使用navicat for sq ...

  7. Python基础-包与模块

    Python基础-包与模块 写在前面 如非特别说明,下文均基于Python3 摘要 为重用以及更好的维护代码,Python使用了模块与包:一个Python文件就是一个模块,包是组织模块的特殊目录(包含 ...

  8. 10个MCU常用的基础知识

    转自:http://bbs.21ic.com/icview-2659278-1-1.html 1.MCU有串口外设的话,在加上电平转换芯片,如MAX232.SP3485就是RS232和RS485接口了 ...

  9. hdu 6354

    Problem Description Edward is a worker for Aluminum Cyclic Machinery. His work is operating mechanic ...

  10. poj 1995 快速幂

    题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11  11=2^0+2^1+2^3    => 3^1*3 ...