• 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
    ",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 }
  • 相关阅读:
    软件开发基本原则(四)—— 风险管理 (转)
    软件开发基本原则(三)—— 基本原则 (转)
    人心散了,项目必然要败! (转)
    项目失败的若干征兆
    项目管理思考——角色转变
    软件开发基本原则(二)—— 典型错误 (转)
    项目管理思考——我适合做项目经理吗
    flex 学习篇 导航类容器
    mfc 中在 dc中 显示 位图
    第一次作业 黎娜 2013551605
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6239057.html
Copyright © 2020-2023  润新知