程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> CF 196 Div 2 D (樹形dp)

CF 196 Div 2 D (樹形dp)

編輯:C++入門知識

CF 196 Div 2 D (樹形dp)


 

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

\


 

 

 

 

 

/*
思路:求出離i這個點最遠的受影響的點作為i這點的屬性,然後當這個屬性小於等於d,那麼這個點可能有鬼

*/

#pragma comment(linker, /STACK:1024000000,1024000000)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf(hihi
)

#define eps 1e-8
typedef __int64 ll;

using namespace std;
#define INF 0x3f3f3f3f
#define N 100005

int vis[N];
int n,m,d;
int head[N];
int num;

int dp[N][2];
int dir[N][2];

struct stud{
  int to,ne;
}e[N*2];

inline void add(int u,int v)
{
    e[num].to=v;
    e[num].ne=head[u];
    head[u]=num++;
}

void dfs(int u,int pre)
{
     if(vis[u]) dp[u][0]=dp[u][1]=0;
     else dp[u][0]=dp[u][1]=-INF;
     dir[u][0]=dir[u][1]=0;

    for(int i=head[u];i!=-1;i=e[i].ne)
    {
        int to=e[i].to;
        if(to==pre) continue;
        dfs(to,u);
        if(dp[to][1]+1>dp[u][0])
        {
            dp[u][0]=dp[to][1]+1;
            dir[u][0]=to;
            if(dp[u][0]>dp[u][1])
            {
                swap(dp[u][0],dp[u][1]);
                swap(dir[u][0],dir[u][1]);
            }
        }
    }
}

void dfss(int u,int pre)
{
     int i,j;
     for(int i=head[u];i!=-1;i=e[i].ne)
     {
         int to=e[i].to;
         if(to==pre) continue;
         if(dir[u][1]==to)
         {
             if(dp[u][0]+1>dp[to][0])
             {
                 dp[to][0]=dp[u][0]+1;
                 dir[to][0]=u;
                 if(dp[to][0]>dp[to][1])
                 {
                     swap(dp[to][0],dp[to][1]);
                     swap(dir[to][1],dir[to][0]);
                 }
             }
         }
         else
             if(dp[u][1]+1>dp[to][0])
             {
                 dp[to][0]=dp[u][1]+1;
                 dir[to][0]=u;
                 if(dp[to][0]>dp[to][1])
                 {
                     swap(dp[to][0],dp[to][1]);
                     swap(dir[to][1],dir[to][0]);
                 }
             }
        dfss(to,u);
     }
}

int main()
{
    int i,j;
    while(~scanf(%d%d%d,&n,&m,&d))
    {
        memset(head,-1,sizeof(head));
        num=0;
        int x;
        memset(vis,0,sizeof(vis));
        for(i=0;i

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved