zoukankan      html  css  js  c++  java
  • 刷题总结——book of evil(codefoeces 337D)

    题目:

    description

    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 nm 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 birepresenting 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.

    题解:

      很妙的一道树形DP

      我们用disd[u][1]表示在u所在子树中且距离节点u受影响的点的距离的最大值···disd[u][2]表示次大值··注意disd[u][1]与disd[u][2]保证两个值来自于两个不同的子树中(后面会知道为什么),disu[u]表示不在u所在子树中的影响点距u的距离的最大值····

      disd[u][1]和disd[u][2]我们可以用一遍DFS求得···而disu我们需要在第二遍dfs时根据父节点来计算儿子节点的值···

      首先设u的其中一个儿子为v,如果disd[v][1]+1==disd[u][1],那么disu[v]=max(disu[u]+1,disd[u][2]+1),否则disu[v]=max(disu[u]+1,disd[u][1]+1)

      由此可以发现disd[u][2]有着给disu[u]的计算做铺垫的作用··三个值计算完后就可以判断了···

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=1e5+5;
    const int inf=0x3f3f3f3f;
    int disd[N][3],disu[N];
    int first[N],next[N*2],go[N*2],tot,n,m,d;
    bool jud[N];
    inline int R()
    {
      char c;int f=0;  
      for(c=getchar();c<'0'||c>'9';c=getchar());
      for(;c<='9'&&c>='0';c=getchar())  f=(f<<3)+(f<<1)+c-'0';
      return f;
    }
    inline void comb(int a,int b)
    {
      next[++tot]=first[a],first[a]=tot,go[tot]=b;
      next[++tot]=first[b],first[b]=tot,go[tot]=a;
    }
    inline void dfs1(int u,int fa)
    {
      if(jud[u])  disd[u][1]=disu[u]=0;
      for(int e=first[u];e;e=next[e])
      {
        int v=go[e];if(v==fa)  continue;
        dfs1(v,u);
        if(disd[v][1]+1>disd[u][1])
        {
          disd[u][2]=disd[u][1];disd[u][1]=disd[v][1]+1;
        }
        else disd[u][2]=max(disd[u][2],disd[v][1]+1);
      }
    }
    inline void dfs2(int u,int fa)
    {
      for(int e=first[u];e;e=next[e])
      {
        int v=go[e];if(v==fa)  continue;
        if(disd[u][1]==disd[v][1]+1)  disu[v]=max(disu[u]+1,disd[u][2]+1);
        else disu[v]=max(disu[u]+1,disd[u][1]+1);
        dfs2(v,u);
      }
    }
    int main()
    {
      //freopen("a.in","r",stdin);
      n=R(),m=R(),d=R();int a,b;  
      for(int i=1;i<=n;i++)  disd[i][1]=disd[i][2]=disu[i]=-inf;
      for(int i=1;i<=m;i++)  a=R(),jud[a]=true;
      for(int i=1;i<n;i++)   a=R(),b=R(),comb(a,b);
      dfs1(1,0);dfs2(1,0);
      int ans=0;
      for(int i=1;i<=n;i++)
      {  
        if(i==1)
        {  
          if(disd[i][1]<=d&&disd[i][2]<=d)  ans++;
        }
        else if(disd[i][1]<=d&&disu[i]<=d)  ans++;
      }
      cout<<ans<<"
    ";
      return 0;
    }
  • 相关阅读:
    原生JavaScript事件详解
    如何真正重写window对象的方法
    JSLint JavaScript代码质量审查工具汉化中文版隆重发布
    {{偷偷告诉你}}本博客已适配移动端浏览
    谷歌(Chrome)浏览器调试JavaScript小技巧
    小米Web前端JavaScript面试题
    根据配置文件加载js依赖模块(JavaScript面试题)
    中移杭州研发中心
    MyBatis与Hibernate区别
    hashmap源码解析,JDK1.8和1.7的区别
  • 原文地址:https://www.cnblogs.com/AseanA/p/7711379.html
Copyright © 2011-2022 走看看