双向DFS模板题
2 seconds
256 megabytes
standard input
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.
The first line contains three space-separated integers n, m and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains mdistinct 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 bi representing the ends of this path.
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.
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
3
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模板题的更多相关文章
- poj1562 Oil Deposits 深搜模板题
题目描述: Description The GeoSurvComp geologic survey company is responsible for detecting underground o ...
- 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 ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- [置顶] 小白学习KM算法详细总结--附上模板题hdu2255
KM算法是基于匈牙利算法求最大或最小权值的完备匹配 关于KM不知道看了多久,每次都不能完全理解,今天花了很久的时间做个总结,归纳以及结合别人的总结给出自己的理解,希望自己以后来看能一目了然,也希望对刚 ...
- 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 ...
- HDU 1874 畅通工程续(模板题——Floyd算法)
题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...
随机推荐
- int_float_double数据类型的存储格式。
一段用来检测编辑器存储方式的程序 //date : 2013/8/16 //designer :pengxiaoen //function check the C programmable langu ...
- Ubuntu无法进入图形界面及VirtualBox扩容的解决方案
升级Ubuntu 12.04后出现“Ubuntu is running in low-graphics mode?”,无法进入图形界面,而且给了一些选项,发现其他几个都没有用,最终只能使用low-gr ...
- Android 开发中eclipse 下 DDMS 视图中 sdcard 中文件导入的处理
首先需要说明下,这里说的sdcard的权限并不是指在Android application程序中设置sdcard的权限读 取问题.而是指在DDMS看到的目录下的那个sdcard目录的权限问题. ...
- docker学习笔记11:Dockerfile 指令 CMD介绍
我们知道,通过docker run 创建并启动一个容器时,命令的最后可以指定容器启动后在容器内立即要执行的指令,如: docker run -i -t ubunu /bin/bash //表示容器 ...
- 不老的新丁 Python何以让人着迷
Python是一门美丽的语言.它简单易学,跨平台,而且运转良好.达成了许多Java一直求索的技术目标.一言以蔽之就是:其他的语言是与时代同 步,而Python则是未雨绸缪,而且计划得颇为出色.当然,这 ...
- 使用 stvd 编译STM8S 时能看到使用RAM ROM大小的方法
刚刚安装的STVD编译器,编译时候不显示用了多少RAM和ROM?对于此问题.有两个方法:一是看.map文件 还有一种是 添加一个补丁,详细操作例如以下,能够在我的资源里下载对应的文件. http:// ...
- PHP - 多文件上传
<html> <head> <meta charset="utf-8"> <title>index_uploads</titl ...
- 网页制作之html基础学习1-简介
学习网页制作主要分为三大块 1.HTML 超文本标记语言( 全称:Hyper Text Markup Language) 专门编辑静态网页 2.CSS 网页美化:是HTML控制的样式 ...
- Home键的获取监听,安卓4.0后就不能在onkeydown方法中获取了。怎么办。
Android下得到Home键按下的消息 在Android下,并不能通过onKeyDown这样的事件来截获Home键的消息,其原因在Android的文档中已经明确的说过了 public stati ...
- api 跳转规则
api 配置: <Context docBase="zjzc-web-api" path="/api" reloadable="false&qu ...