题目:

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. keycode对应表

    keycode对应表 keycode    8 = BackSpace 回格keycode    9 = Tab keycode   12 = Clearkeycode   13 = Enter 回车 ...

  2. php循环a-z字母表

    ord — 返回字符的 ASCII 码值 说明 int ord ( string $string ) 返回字符串 string 第一个字符的 ASCII 码值.  该函数是 chr() 的互补函数. ...

  3. 一、git提交代码步骤

    git add .   //添加到本地缓存区 git commit -m '备注 ' //提交代码到本地仓库 git push  //将本地代码推送到develop分支上 git checkout m ...

  4. C++ Stack 与String

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

  5. Java制作桌面弹球下载版 使用如鹏游戏引擎制作 包含2个精灵球同时弹动

    package com.swift; import com.rupeng.game.GameCore; public class DesktopBouncingBall implements Runn ...

  6. 2d游戏中的射线与矩形检测碰撞

    cc.exports.LineCollideRect(startLine,endLine,rect)--向量与矩形检测碰撞 --获取矩形的四个顶点位置 local p = {cc.p(rect.x,r ...

  7. PHP使用FTP上传文件到服务器(实战篇)

    我们在做开发的过程中,上传文件肯定是避免不了的,平常我们的程序和上传的文件都在一个服务器上,我们也可以使用第三方sdk上传文件,但是文件在第三方服务器上.现在我们使用PHP的ftp功能把文件上传到我们 ...

  8. 将远程分支拷贝到本地,并更新代码push到原分支

    第一步:git clone +主分支 第二步:git fetch origin 分支名 第三步:git checkout -b 分支名 origin/分支名 第四步:git pull origin 分 ...

  9. Python + Bottle + 谷歌搜索Api 实现简单搜索引擎

    1.运行环境 python3 centos7 2.Bottle的使用 使用bottle主要是因为它仅用python自带的库即可实现对web的搭建. bottle源码分析 bottle使用教程 3.代码 ...

  10. Vim如何显示和关闭行号

    显示行号: set nu 去除行号: set nonu