zoukankan      html  css  js  c++  java
  • Book of Evil CodeForces

    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.

    Example

    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.

    题解:秒啊,显然不能暴力,不过怎么转化这个问题呢?记已知被影响的点集为S,找S中相距最远的两个点a,b,然后分别以a,b为根,算出各点到根的距离,最终问题也就变成了找满足(d1[i]<=d&&d2[i]<=d)的点的个数。怎么找相距最远的点,本人也不是很明白。。。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=1e5+5;
     5 
     6 int n,m,d;
     7 int p[maxn],d1[maxn],d2[maxn];
     8 
     9 vector<int> G[maxn];
    10 
    11 void DFS(int pa,int u,int deep,int d[]){
    12     d[u]=deep;
    13     for(int i=0;i<G[u].size();i++){
    14         int v=G[u][i];
    15         if(pa!=v) DFS(u,v,deep+1,d);
    16     }
    17 }
    18 
    19 int main()
    20 {   scanf("%d%d%d",&n,&m,&d);
    21     for(int i=1;i<=m;i++) scanf("%d",&p[i]);
    22     for(int i=1;i<=n;i++){
    23         int a,b;
    24         scanf("%d%d",&a,&b);
    25         G[a].push_back(b);
    26         G[b].push_back(a);    
    27     }
    28     DFS(0,1,0,d1);
    29     int temp=0;
    30     for(int i=1;i<=m;i++) if(d1[p[i]]>d1[temp]) temp=p[i];
    31     DFS(0,temp,0,d1);
    32     temp=0;
    33     for(int i=1;i<=m;i++) if(d1[p[i]]>d1[temp]) temp=p[i];
    34     DFS(0,temp,0,d2);
    35     
    36     int ans=0;
    37     for(int i=1;i<=n;i++) if(d1[i]<=d&&d2[i]<=d) ans++;
    38     cout<<ans<<endl;
    39     
    40 }
  • 相关阅读:
    原生,js,自执行函数的三种写法,及特性
    js延迟加载的六种方式
    antd table 合并行的方法
    css 常用样式antd design 中table,表格省略号处理的问题
    axios 中文文档
    js ,几张遍历,循环的方法
    javascript怎么保留两位小数
    Category(十九)
    Protocol(协议)(二十)
    Extension延展(十八)
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7742534.html
Copyright © 2011-2022 走看看