CodeForces 337D——Book of Evil(数据结构)

来源:互联网 发布:如何投资理财 知乎 编辑:程序博客网 时间:2024/05/16 16:56

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.

Examples
input
6 2 31 21 52 33 44 55 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,为可能的力量源有几个。

思路:相当于是找到距离这m的点的距离都不小于d的点的个数。

先从任意一个点一次dfs,找到m个点中距离最远的点,标记为max1,在以max1开始,dfs一遍,从数组d1记录各个点的距离,找到距离最远的点max2,再从max2开始跑一遍dfs,用d2记录距离。遍历所有点,到max1和max2的距离都小于d的点就成立。

#include<cstdio>#include<cstring>#include <string>#include <iostream>#include <algorithm>#include <vector>#include <map>#include <set>using namespace std;#define MAXN 200010#define INF 1000000000int d1[MAXN],d2[MAXN];int h[MAXN];int head[MAXN];int p[MAXN];set <int> s;vector <int> ans;struct edge{int v;int next;};edge G[MAXN*2];int num=0;int vis[MAXN];void add(int u,int v){G[num].v=v;G[num].next=head[u];head[u]=num++;};void dfs(int u,int d[]){    vis[u]=true;    for(int k=head[u];k!=-1;k=G[k].next){        int v=G[k].v;        if(!vis[v]){            d[v]=d[u]+1;            dfs(v,d);        }    }}int n,m,d;int main(){    scanf("%d%d%d",&n,&m,&d);    for(int i=0;i<m;i++){        scanf("%d",p+i);        s.insert(p[i]);    }    for(int i=1;i<=n;i++){        if(s.count(i)==0)            ans.push_back(i);    }    memset(head,-1,sizeof(head));    memset(vis,0,sizeof(vis));    for(int i=0;i<n-1;i++){        int a,b;        scanf("%d%d",&a,&b);        add(a,b);        add(b,a);    }    h[1]=0;    dfs(1,h);    int max1=0;    for(int i=0;i<m;i++){        if(h[p[i]]>h[p[max1]])            max1=i;    }    memset(vis,0,sizeof(vis));    d1[p[max1]]=0;    dfs(p[max1],d1);    int max2=0;    for(int i=0;i<m;i++){        if(d1[p[i]]>d1[p[max2]])            max2=i;    }    memset(vis,0,sizeof(vis));    d2[p[max2]]=0;    dfs(p[max2],d2);    int cnt=0;    for(int i=1;i<=n;i++){        if(d1[i]<=d&&d2[i]<=d)            cnt++;    }    printf("%d\n",cnt);    return 0;}









0 0
原创粉丝点击