CF 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 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 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.

题意:有n个点,(n-1)条边,m个鬼,求存在多少个点使得这个点到每个鬼的距离都不超过d。

题解:找出两个鬼之间的最远距离(类似树的直径,直径两端都为鬼,设为A,B),如果某一点到A, B距离都不超过d,那么其余的鬼肯定也不会超过d。所以先找出距离最远的端点A,B,再求出各个点到A, B的距离分为别da[ ],db[ ],如果da[i],db[i],都不超过d,则 i点符合要求。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
struct node
{
   int v,next;
   node(){}
   node(int a,int b) {v=a;next=b;}
}e[maxn<<1];
int head[maxn],da[maxn],db[maxn],ghost[maxn],n,m,d,cnt,A,B,mmax;
void dfs1(int s,int fa,int deep);
void dfs2(int s,int fa,int d[],int deep);
int main()
{
  int i,x,u,v,ans=0;
  fill(head,head+maxn,-1);
  scanf("%d%d%d",&n,&m,&d);
  for(i=0;i<m;i++)
  {
    scanf("%d",&x);
    ghost[x]=1;
  }
  for(i=1;i<n;i++)
  {
    scanf("%d%d",&u,&v);
    e[++cnt]=node(v,head[u]);head[u]=cnt;
    e[++cnt]=node(u,head[v]);head[v]=cnt;
  }
  mmax=0;
  dfs1(1,-1,0);
  mmax=0;
  dfs2(A,-1,da,0);
  dfs2(B,-1,db,0);
  for(i=1;i<=n;i++) if(da[i]<=d&&db[i]<=d) ans++;
  printf("%d
",ans);
  return 0;
}
void dfs1(int s,int fa,int deep)
{
   int i,v;
   if(ghost[s]&&deep>mmax) A=s,mmax=deep;
   for(i=head[s];i!=-1;i=e[i].next)
    {
      v=e[i].v;
      if(v==fa) continue;
      dfs1(v,s,deep+1);
    }
}
void dfs2(int s,int fa,int d[],int deep)
{
   int i,v;
   d[s]=deep;
   if(d==da&&ghost[s]&&mmax<deep) B=s,mmax=deep;
   for(i=head[s];i!=-1;i=e[i].next)
   {
     v=e[i].v;
     if(v==fa) continue;
     dfs2(v,s,d,deep+1);
   }
}
本博客仅为本人学习,总结,归纳,交流所用,若文章中存在错误或有不当之处,十分抱歉,劳烦指出,不胜感激!!!
原文地址:https://www.cnblogs.com/VividBinGo/p/11344960.html