Book of Evil CodeForces - 337D

在一个由n个定居点构成的沼泽区域,一位圣骑士正在追踪一本古老的邪恶之书。通过已知受其影响的m个定居点信息及损害范围d,本文介绍了一种算法来确定可能藏匿该书的位置。

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.

Example

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.

题解:秒啊,显然不能暴力,不过怎么转化这个问题呢?记已知被影响的点集为S,找S中相距最远的两个点a,b,然后分别以a,b为根,算出各点到根的距离,最终问题也就变成了找满足(d1[i]<=d&&d2[i]<=d)的点的个数。怎么找相距最远的点,本人也不是很明白。。。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=1e5+5;
 5 
 6 int n,m,d;
 7 int p[maxn],d1[maxn],d2[maxn];
 8 
 9 vector<int> G[maxn];
10 
11 void DFS(int pa,int u,int deep,int d[]){
12     d[u]=deep;
13     for(int i=0;i<G[u].size();i++){
14         int v=G[u][i];
15         if(pa!=v) DFS(u,v,deep+1,d);
16     }
17 }
18 
19 int main()
20 {   scanf("%d%d%d",&n,&m,&d);
21     for(int i=1;i<=m;i++) scanf("%d",&p[i]);
22     for(int i=1;i<=n;i++){
23         int a,b;
24         scanf("%d%d",&a,&b);
25         G[a].push_back(b);
26         G[b].push_back(a);    
27     }
28     DFS(0,1,0,d1);
29     int temp=0;
30     for(int i=1;i<=m;i++) if(d1[p[i]]>d1[temp]) temp=p[i];
31     DFS(0,temp,0,d1);
32     temp=0;
33     for(int i=1;i<=m;i++) if(d1[p[i]]>d1[temp]) temp=p[i];
34     DFS(0,temp,0,d2);
35     
36     int ans=0;
37     for(int i=1;i<=n;i++) if(d1[i]<=d&&d2[i]<=d) ans++;
38     cout<<ans<<endl;
39     
40 }

 

 

转载于:https://www.cnblogs.com/zgglj-com/p/7742534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值