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

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

题意:找离所有给定的点的距离都不超过d的点的个数;

思路:树形DP;

找到每个点的距离最远的给定的点,由于给定的是一棵树,然后剩下的就是和http://www.cnblogs.com/zzuli2sjy/p/6232574.html这题一样;

  1 #include<stdio.h>
  2 #include<math.h>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<string.h>
  6 #include<iostream>
  7 #include<stack>
  8 #include<vector>
  9 using namespace std;
 10 typedef long long LL;
 11 vector<int>vec[200005];
 12 bool flag[200005];
 13 void Init();
 14 typedef struct node
 15 {
 16     int id1;
 17     int cost1;
 18     int id2;
 19     int cost2;
 20 } ss;
 21 ss dp[200005];
 22 bool ff[200005];
 23 void dfs(int n);
 24 void dfs2(int n);
 25 int main(void)
 26 {
 27     int n,m,d;
 28     while(scanf("%d %d %d",&n,&m,&d)!=EOF)
 29     {
 30         int x,y;
 31         memset(ff,0,sizeof(ff));
 32         for(int i = 0; i < m; i++)
 33             scanf("%d",&x),ff[x] = true;
 34         Init();
 35         for(int i = 0; i < n-1; i++)
 36         {
 37             scanf("%d %d",&x,&y);
 38             vec[x].push_back(y);
 39             vec[y].push_back(x);
 40         }
 41         dfs(1);
 42         memset(flag,0,sizeof(flag));
 43         dfs2(1);
 44         int cn = 0;
 45         for(int i = 1; i <= n; i++)
 46         {
 47             if(dp[i].cost1<=d)
 48             {
 49                 cn++;
 50             }
 51         }
 52         printf("%d\n",cn);
 53     }
 54     return 0;
 55 }
 56 void Init()
 57 {
 58     for(int i = 0; i < 200005; i++)
 59         vec[i].clear();
 60     memset(flag,0,sizeof(flag));
 61     for(int i = 0; i <= 200000; i++)
 62     {
 63         dp[i].cost1  = 0,dp[i].cost2 = 0;
 64         dp[i].id2 = -1,dp[i].id1 = -1;
 65         if(ff[i])dp[i].id1 = i,dp[i].id2 = i;
 66     }
 67 }
 68 void dfs(int n)
 69 {
 70     flag[n] = true;
 71     for(int i = 0; i < vec[n].size(); i++)
 72     {
 73         int id = vec[n][i];
 74         if(!flag[id])
 75         {
 76             dfs(id);
 77             if(dp[id].id1!=-1)
 78             {
 79                 if(dp[id].cost1+1 > dp[n].cost1)
 80                 {
 81                     dp[n].cost2 = dp[n].cost1;
 82                     dp[n].id2 = dp[n].id1;
 83                     dp[n].cost1 = dp[id].cost1+1;
 84                     dp[n].id1 = id;
 85                 }
 86                 else if(dp[id].cost1+1 > dp[n].cost2)
 87                 {
 88                     dp[n].cost2 = dp[id].cost1+1;
 89                     dp[n].id2 = id;
 90                 }
 91             }
 92         }
 93     }
 94 }
 95 void dfs2(int n)
 96 {
 97     flag[n] = true;
 98     for(int i = 0 ; i < vec[n].size(); i++)
 99     {
100         int id = vec[n][i];
101         if(!flag[id])
102         {
103             if(dp[n].id1!=-1&&dp[n].id1 != id)
104             {
105                 if(dp[n].cost1 + 1 > dp[id].cost1)
106                 {
107                     dp[id].cost2 = dp[id].cost1;
108                     dp[id].id2 = dp[id].id1;
109                     dp[id].cost1 = dp[n].cost1+1;
110                     dp[id].id1 = n;
111                 }
112                 else  if(dp[n].cost1 + 1 > dp[id].cost2)
113                 {
114                     dp[id].cost2 = dp[n].cost1+1;
115                     dp[id].id2 = n;
116                 }
117             }
118             else if(dp[n].id1!=-1&&dp[n].id1 == id&&dp[n].id2!=-1)
119             {
120                 if(dp[n].cost2 + 1 > dp[id].cost1)
121                 {
122                     dp[id].cost2 = dp[id].cost1;
123                     dp[id].id2  = dp[id].id1;
124                     dp[id].cost1 = dp[n].cost2+1;
125                     dp[id].id1 = n;
126                 }
127                 else if(dp[n].cost2 + 1>dp[id].cost2)
128                 {
129                     dp[id].cost2 = dp[n].cost2+1;
130                     dp[id].id2  = n;
131                 }
132             }
133             dfs2(id);
134         }
135     }
136 }

 

转载于:https://www.cnblogs.com/zzuli2sjy/p/6239057.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值