现在的位置: 首页 > 综合 > 正文

CF 337D(Book of Evil-distdown[]-distup[])

2013年10月02日 ⁄ 综合 ⁄ 共 3652字 ⁄ 字号 评论关闭
D. 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 ≤ 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.

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.

给定一堆点,求到任意的给定点的距离≤d的点有几个?

本题是树的圆的交

需要2个数组distdown[]&distdown[]

表示第i个点向上和向下距离的最远给定点。。。


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
#define MAXM (200000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,m,d,distdown[MAXN],distup[MAXN],dismax[MAXN][2];
bool mark[MAXN]={0};
int edge[MAXM],next[MAXM]={0},pre[MAXN]={0},size=1;
void modi(int &a,int &b,int c)
{
	if (c>=a) b=a,a=c;
	else if (c>=b) b=c;
}
void addedge(int u,int v)
{
	edge[++size]=v;
	next[size]=pre[u];
	pre[u]=size;
}
void addedge2(int u,int v){addedge(u,v),addedge(v,u);}
void dfs1(int x,int fa)
{
	if (mark[x]) distdown[x]=0;
	Forp(x)
	{
		int v=edge[p];
		if (v^fa)
		{
			dfs1(v,x);
			if (distdown[v]>=0)
			{
				distdown[x]=max(distdown[x],distdown[v]+1);
				modi(dismax[x][0],dismax[x][1],distdown[v]);
			}
		}
	}
}
void dfs2(int x,int fa)
{
	if (mark[x]) distup[x]=0;
	if (fa)
	{
		if (distup[fa]>=0) distup[x]=max(distup[x],distup[fa]+1);
		if (dismax[fa][0]^distdown[x]) distup[x]=max(distup[x],dismax[fa][0]+2);
		else distup[x]=max(distup[x],dismax[fa][1]+2);
	}		
	Forp(x)
	{
		int &v=edge[p];
		if (v^fa)
		{
			dfs2(v,x);
		}
	}
}
int main()
{
//	freopen("Evil.in","r",stdin);
	cin>>n>>m>>d;
	For(i,m)
	{
		int p;cin>>p;mark[p]=1;
	}
	MEMi(distdown)MEMi(distup)
	MEMi(dismax)
	
	For(i,n-1)
	{
		int u,v;
		cin>>u>>v;
		addedge2(u,v);
	}
	dfs1(1,0);
	dfs2(1,0);
	
	int ans=0;
	For(i,n) if (max(distdown[i],distup[i])<=d) ans++;//cout<<i<<' ';
	cout<<ans<<endl;
	return 0;
}




抱歉!评论已关闭.