Codeforces 337D Book of Evil

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 n, m and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains m distinct 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

Copy

6 2 3
1 2
1 5
2 3
3 4
4 5
5 6

Output

Copy

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.

题目解法:

我们首先以1节点为根节点,算出每个点的此时的深度,然后找出受影响的点中,深度最大的一个节点u;再以这个节点为根节点重新算一次每个点的深度(用数组d1保存),同样找出此时受影响的点中深度最大的一个点v。那么此时u,v这两个点一定是之前说过的两两距离最远的点。又因为d1数组中保存的是每个节点到根节点(也就是u)的距离,我们可以再做一次DFS,以v为节点,算出每个节点的深度(用数组d2保存)。那最后,只需要遍历一遍各节点,统计有多少符合条件(d1[i]<=d&&d2[i]<=d)的节点即可!

AC代码

#include <iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

#define N 100003
vector<int>map[N];
vector<int>p;
int vis[N];
int flag[N];
int dis1[N];
int dis2[N];
int max1=0;
int maxdis1=0;
int max2=0;
int maxdis2=0;

void dfs(int start,int step,int dis[])
{
    vis[start]=1;
    for(int i=0;i<map[start].size();++i)
    {
        int temp=map[start][i];
        if(vis[temp]==0)
        {
            dis[temp]=step+1;
            dfs(temp,step+1,dis);
        }
    }
}

int main()
{
    int n,m,d;
    cin>>n>>m>>d;
    for(int i=1;i<=m;++i)
    {
        int x;
        cin>>x;
        p.push_back(x);
    }
    for(int i=1;i<n;++i)
    {
        int x,y;
        cin>>x>>y;
        map[x].push_back(y);
        map[y].push_back(x);
    }
    dfs(1,0,dis1);
    int k=0;
    for(int i=0;i<m;++i)
    {
        if(dis1[p[i]]>dis1[p[k]])
            k=i;
    }
    max1=p[k];
    memset(vis,0,sizeof(vis));
    memset(dis1,0,sizeof(dis1));
    dfs(max1,0,dis1);
    k=0;
    for(int i=0;i<m;++i)
        if(dis1[p[i]]>dis1[p[k]])
            k=i;
    max2=p[k];
    memset(vis,0,sizeof(vis));
    dfs(max2,0,dis2);
    int ans=0;
    for(int i=1;i<=n;++i)
        if(dis1[i]<=d&&dis2[i]<=d)
            ans++;
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36921652/article/details/81385723