Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud
Book of Evil
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 the 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 and settlements are the minimum number of paths that has to be CRO Ssed to get from one settlement to the other one. Manao knows the book of Evil have got a damage range D . This means a if the book of Evil are located in some settlement, it damage (for example, emergence of ghosts and Werewo Lves) affects other settlements at distance D or less from the settlement where the book R Esides.
Manao have heard of m settlements affected by the book of Evil. Their numbers is p1, P2, ..., Pm. Note that the book could be affecting and other settlements as well, but this has a not been detected yet. Manao wants to determine which settlements could 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 P 1, p 2, ..., p m (1≤ p i ≤ 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 a I and b I representing the ends of this path.
Output
Print a single number-the number of settlements so may contain the book of Evil. It is possible that Manao received some controversial information and there are 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 Evil equals 3 and its effects has been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
Test instructions
to a tree of n nodes, there is a "Book of Evil" at some point in the tree, and the points around it that are less than or equal to D are affected, and the points that are known to M are affected, and several may have "Book of Evil"
By maintaining the maximum distance from a node to its subtree, and removing the longest distance from the longest, to another subtree, and then DFS again.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <vector>6 using namespacestd;7 #defineMAXN 1000108vector<int>G[MAXN];9 intdp[maxn][2];Ten intpre[maxn][2]; One intans=0; A intD; - voidDfsintUintFA) { - for(intI=0; I<g[u].size (); i++){ the intv=G[u][i]; - if(V==FA)Continue; - DFS (v,u); - intd1=dp[v][0]+1; + if(d1>dp[u][1]){ -dp[u][1]=D1; +pre[u][1]=v; A if(dp[u][1]>dp[u][0]){ atSwap (dp[u][0],dp[u][1]); -Swap (pre[u][0],pre[u][1]); - } - } - } - } in voidDFS2 (intUintFaintdis) { - if(dis>d)return; to if(dp[u][0]<=d) ans++;//Cout<<u<<endl;} + intD1=max (dp[u][0],dis) +1; - intD2=max (dp[u][1],dis) +1; the for(intI=0; I<g[u].size (); i++) * { $ intv=G[u][i];Panax Notoginseng if(V==FA)Continue; - if(pre[u][0]==v) dfs2 (V,U,D2); the ElseDFS2 (V,U,D1); + } A } the intMain () + { -Ios::sync_with_stdio (false); $ //freopen ("in.in", "R", stdin); $ intM,n; - intu,v; -Cin>>n>>m>>D; the for(intI=0; i<n;i++) dp[i][0]=dp[i][1]=-MAXN; - for(intI=0; i<n;i++) pre[i][0]=pre[i][1]=-1;Wuyi for(intI=0; i<m;i++){ theCin>>u; -u--; Wudp[u][0]=dp[u][1]=0; - } About for(intI=0; i<n;i++) g[i].clear (); $ for(intI=0; i<n-1; i++){ -Cin>>u>>v; -u--;v--; - G[u].push_back (v); A g[v].push_back (u); + } theans=0; -Dfs0,-1); $DFS2 (0,-1,-MAXN); thecout<<ans<<Endl; the return 0; the}
code June
Codeforces 337D Book of Evil (DP)