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 }
  • 相关阅读:
    去中心化存储的QoS是什么?
    谈如何真正地做好去中心存储?
    Kowala协议:一组分布式,自我调节,资产跟踪特性的加密货币(二)
    MySQL主从备份
    Java面试总结-基础篇2
    Java面试总结-基础篇1
    锁定网络问题的方法
    我是如何在实际项目中解决MySQL性能问题
    serialVersionUID的作用
    Windows和Linux下如何自动备份MySQL
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7742534.html
Copyright © 2011-2022 走看看