Book of Evil 树双向DFS
Book of Evil
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;
}
Book of Evil 树双向DFS的更多相关文章
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- [2]树的DFS序
定义: 树的DFS序就是在对树进行DFS的时候,对树的节点进行重新编号:DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题. 代码: void ...
- 树的dfs序 && 系统栈 && c++ rope
利用树的dfs序解决问题: 就是dfs的时候记录每个节点的进入时间和离开时间,这样一个完整的区间就是一颗完整的树,就转化成了区间维护的问题. 比如hdu3887 本质上是一个求子树和的问题 #incl ...
- CF877E Danil and a Part-time Job 线段树维护dfs序
\(\color{#0066ff}{题目描述}\) 有一棵 n 个点的树,根结点为 1 号点,每个点的权值都是 1 或 0 共有 m 次操作,操作分为两种 get 询问一个点 x 的子树里有多少个 1 ...
- HDU 1269.迷宫城堡-Tarjan or 双向DFS
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU4117 GRE WORDS(AC自动机+线段树维护fail树的dfs序)
Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the mos ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
- 送礼物(二分加双向DFS)
题目链接 题意:给你n个礼物重量,给你一个M力量,看你一次性搬动不超过M的礼物重量. 思路:看似背包,但M太大.所以要用DFS,但n也有45,所以考虑双向DFS先搜前半部分满足情况的所有重量,然后去重 ...
- bzoj 3551 [ONTAK2010]Peaks加强版(kruskal,主席树,dfs序)
Description [题目描述]同3545 Input 第一行三个数N,M,Q. 第二行N个数,第i个数为h_i 接下来M行,每行3个数a b c,表示从a到b有一条困难值为c的双向路径. 接下来 ...
随机推荐
- VS2013无法链接到TFS (转)
VS2013无法链接到TFS(Visual studio online),错误TF31001,TF31002 TF31002: Unable to connect to VisualStudio ...
- hdu3507
题意: 给n(n<=10^6)个非负数字,放在一个数组num中,再给一个特殊值m.求将这个数组分成任意多个区间,每个区间[a,b]的值定义为( sigma(num[i] | (a<=i&l ...
- 在Eclipse中使用Maven构建SpringMVC项目
环境搭建 安装JDK, Eclipse, Tomcat等 – 请参考网上常见攻略. 安装Maven: 下载需要的Maven 版本( http://maven.apache.org/download.c ...
- springmvc结合freemarker,非自定义标签
参考:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/ 上图: 目录层级: 启动后的访问地址:http://localhos ...
- Effective C++学习笔记——构造/析构/拷贝运算
条款9:决不再构造和析构过程中调用virtual函数,包括通过函数间接调用virtual函数. 应用:想在一个继承体系中,一个derived class被创建时,某个调用(例如生成相应的日志log)会 ...
- 实现将VirtualBox 虚拟机转换为KVM虚拟机的步骤
原来在桌面上一直使用virtualbox虚拟机管理程序(VMM)构建虚拟机安装不同的操作系统,现在 研究linux下的KVM,能否将已经建立的virtualBox虚拟客户机(guest)转换为KVM虚 ...
- 64位WINDOWS系统环境下应用软件开发的兼容性问题(CPU 注册表 目录)
应用软件开发的64 位WINDOWS 系统环境兼容性 1. 64 位CPU 硬件 目前的64位CPU分为两类:x64和IA64.x64的全称是x86-64,从名字上也可以看出来它和 x86是兼容的,原 ...
- 解决Flex4 发布后访问 初始化极其缓慢的问题
原文http://blog.163.com/vituk93@126/blog/static/170958034201282222046364/ 昨天找了个免费.net空间,想测试一下做的一个简单Fle ...
- 20个经典bootsrtap后台html网站模板推荐
今天为大家推荐20款不同风格的Bootstrap后台管理模板,每一款都经典可用,能预览和下载,保证让你挑得眼花缭乱. 1,Simpli flag蓝色 Simpli Flat蓝色管理模板是一款采用Fla ...
- HDU 4417 Super Mario
题解:函数式线段树求区间小于等于k的数有几个,离线做法,首先将所有询问和序列一起离散,然后用函数式线段树处理. #include <map> #include <cstdio> ...