CF 196 Div 2 D (tree dp)

Source: Internet
Author: User

CF 196 Div 2 D (tree dp)

 

D. Book of edevil 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 specified ent Book of edevil in a swampy area. This area containsNSettlements numbered from 1N. Moving through the swamp is very difficult, so people tramped exactlyNProcedure-Procedure 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 travsing erone 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 edevil has got a damage rangeD. This means that if the Book of edevil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distanceDOr less from the settlement where the Book resides.

Manao has heardMSettlements affected by the Book of edevil. Their numbers areP1, bytes,P2, middle..., middle ,...,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 in the Book. Help him with this difficult task.

Input

The first line contains three space-separated integersN,MAndD(1 digit ≤ DigitMLimit ≤ limitNMemory ≤ memory 100000; 0 memory ≤ memoryDLimit ≤ limitNSeconds-rows 1). The second line containsMDistinct space-separated integersP1, bytes,P2, middle..., middle ,...,PM(1 digit ≤ DigitPILimit ≤ limitN). ThenNLimits-limits 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integersAIAndBIRepresenting the ends of this path.

Output

Print a single number-the number of settlements that may contain the Book of edevil. it is possible that Manao has ed some controversial information and there is no settlement that may contain in the Book. in such case, print 0.

Sample test (s) input
6 2 31 21 52 33 44 55 6
Output
3
Note

Sample 1. The damage range of the Book of edevil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.



 

 

 

 

 

/* Train of Thought: Find the most affected point from the point I as the attribute of I, And then when this attribute is less than or equal to d, so this point may have a ghost */# pragma comment (linker,/STACK: 1024000000,1024000000) # 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-8typedef _ int64 ll; using namespace std; # define INF 0x3f3f3f # define N Limit 5int 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] =; 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] =) {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 mai N () {int I, j; while (~ Scanf (% d, & n, & m, & d) {memset (head,-1, sizeof (head); num = 0; int x; memset (vis, 0, sizeof (vis); for (I = 0; I
          
           

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.