B. Book of Evil
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ mn ≤ 100000; 0 ≤ dn - 1). The second line contains mdistinct space-separated integers p1, p2, ..., pm (1 ≤ pin). 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.

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.

Sample test(s)
input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
output
3
Note

Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <bitset>
#include <fstream> typedef unsigned long long ull;
#define mp make_pair
#define pb push_back const long double eps = 1e-9;
const double pi = acos(-1.0);
const long long inf = 1e18; using namespace std; int n, m, d;
int f[ 100100 ], g[ 100100 ];
vector< int > graf[ 100100 ];
bool ok[ 100100 ]; void dfs1( int v, int p )
{
//cout << v << " " << p << endl;
f[v] = -1;
for ( int i = 0; i < graf[v].size(); i++ )
{
int next = graf[v][i]; if ( next == p ) continue;
dfs1( next, v );
f[v] = max( f[v], ( f[next] == -1 ? -1 : f[next] + 1 ) );
}
if ( ok[v] ) f[v] = max( 0, f[v] );
} void dfs2( int v, int p, int root )
{
g[v] = root;
vector< int > sons;
sons.pb( ( root == -1 ? -1 : root + 1 ) );
if ( ok[v] ) sons.pb( 1 );
for ( int i = 0; i < graf[v].size(); i++ )
{
int next = graf[v][i]; if ( next == p ) continue;
sons.pb( ( f[next] == -1 ? -1 : f[next] + 2 ) );
}
sort( sons.begin(), sons.end(), greater<int>() );
for ( int i = 0; i < graf[v].size(); i++ )
{
int next = graf[v][i]; if ( next == p ) continue;
int nroot = sons[1];
if ( ( f[next] == -1 ? -1 : f[next] + 2 ) != sons[0] ) nroot = sons[0];
dfs2( next, v, nroot );
}
} int main (int argc, const char * argv[])
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
scanf("%d%d%d", &n, &m, &d);
for ( int i = 1; i <= m; i++ )
{
int a; scanf("%d", &a);
ok[a] = true;
}
for ( int i = 1; i < n; i++ )
{
int a, b; scanf("%d%d", &a, &b);
graf[a].pb(b);
graf[b].pb(a);
}
dfs1( 1, -1 );
dfs2( 1, -1, -1 );
int ans = 0;
for ( int i = 1; i <= n; i++ ) if ( max( f[i], g[i] ) <= d ) ans++;
//for ( int i = 1; i <= n; i++ ) cout << i << " " << f[i] << " " << g[i] << endl;
cout << ans << endl;
return 0;
}

双向DFS模板题的更多相关文章

  1. poj1562 Oil Deposits 深搜模板题

    题目描述: Description The GeoSurvComp geologic survey company is responsible for detecting underground o ...

  2. hdu 2586 How far away ?(LCA - Tarjan算法 离线 模板题)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  4. [置顶] 小白学习KM算法详细总结--附上模板题hdu2255

    KM算法是基于匈牙利算法求最大或最小权值的完备匹配 关于KM不知道看了多久,每次都不能完全理解,今天花了很久的时间做个总结,归纳以及结合别人的总结给出自己的理解,希望自己以后来看能一目了然,也希望对刚 ...

  5. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  6. HDU 1874 畅通工程续(模板题——Floyd算法)

    题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...

  7. HDU 2544 最短路(模板题——Floyd算法)

    题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...

  8. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  9. bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题

    [ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...

随机推荐

  1. 在eclipse上安装 sdk出现的各种问题

    在eclipse上下进行android开发需要  有android SDK 和ADT 一般adt版本瑶台低, 会被提示安装较高版本的ADT,  不然, SDK可能无法使用 在安装 SDK过程中出现这样 ...

  2. C#验证码的另一种操作方法

    sb = new StringBuilder(); char c = '0'; string s = ""; for (int i = 0; i < 4; i++) { Ra ...

  3. nginx提示:500 Internal Server Error错误的解决方法

    现在越来越多的站点开始用 Nginx ,("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 ...

  4. 文件打包bundle

    做项目时,经常会有一些资源拷来拷去会很麻烦,打包这些文件到项目里会方便很多! 首先把文件放到一个文件夹里                然后重命名文件夹为iToast.bundle 拖到项目里 如何访 ...

  5. 04-IOSCore - User Defaults、Archive、存储总结

    一. User Defaults 1. 是什么? 是一个特殊的plist文件 2. 干什么? 用于保存应用的配置信息 3. 存什么信息? 信息:欢迎界面有没有被打开过 目的:欢迎界面只显示一次 信息: ...

  6. extern、static、auto、register 定义变量的不同用法

    首先得说明什么叫“编译单元”.每个 .c 文件会被编译为一个 .o 文件,这个就是一个编译单元.最后所有的编译单元被链接起来,就是一个库或一个程序. 一个变量/函数,只要是在全局声明的,链接之后都隐含 ...

  7. 动态网页爬取例子(WebCollector+selenium+phantomjs)

    目标:动态网页爬取 说明:这里的动态网页指几种可能:1)需要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成,如一个html里有<div id="test" ...

  8. 基于Andoird 4.2.2的Account Manager源代码分析学习:AccountManagerService系统服务的添加

    从启动说起 Android系统加载时,首先启动init进程,该进程会启动Zygote进程.Zygote进程执行/system/bin/app_process程序.app_process程序在执行中,通 ...

  9. 线程知识-ThreadLocal使用详解

    最近在看Spring的时候回顾了一下ThreadLocal,下面是ThreadLocal的使用说明. 概述 首先,谈到ThreadLocal的使用,我们先来了解一下ThreadLocal是什么?Thr ...

  10. 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)-大壮他哥

    步骤: 1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft ...