• Codeforces Round #321 (Div. 2) C dfs处理(双向边叶子节点的判断)


    C. Kefa and Park
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Kefa decided to celebrate his first big salary by going to the restaurant.

    He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.

    The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats.

    Your task is to help Kefa count the number of restaurants where he can go.

    Input

    The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.

    The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).

    Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge.

    It is guaranteed that the given set of edges specifies a tree.

    Output

    A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.

    Examples
    Input
    4 1
    1 1 0 0
    1 2
    1 3
    1 4
    Output
    2
    Input
    7 1
    1 0 1 1 0 0 0
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    Output
    2
    Note

    Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.

    Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.

    Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.

    题意:给一棵树  告诉你n个节点上分别是否有猫 从根节点开始到叶子节点的路径上最多能连续经过m只猫

       问从根节点,能够到达多少个叶子节点

    题解:dfs处理  搜索的同时 ,记录连续经过的猫的最大值

            有一点技巧处理,如何判断叶子节点的问题 :因为存储的是双向边,除了根节点,

              只有叶子节点的父亲节点的个数为1 标记处理一下。就可以判断出叶子节点。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 int nedge;
     6 int n,m;
     7 int pre[100005];
     8 int have[100005];
     9 int used[100005];
    10 int a,b,c;
    11 int ans=0;
    12 int vis[100005];
    13 struct node
    14 {
    15     int to;
    16     int w;
    17     int pre;
    18 }N[200005];
    19 void add(int aa,int bb,int cc)
    20 {
    21      nedge++;
    22      N[nedge].to=bb;
    23      N[nedge].w=cc;
    24      N[nedge].pre=pre[aa];
    25      pre[aa]=nedge;
    26 }
    27 void dfs(int s,int jishu,int zhi)
    28 {
    29     if(s!=1&&zhi<=m&&vis[s]<2)
    30         {
    31             ans++;
    32             return ;
    33         }
    34     for(int i=pre[s];i;i=N[i].pre)
    35     {
    36         if(!used[N[i].to])
    37         {
    38             used[N[i].to]=1;
    39             if(have[N[i].to])
    40                 dfs(N[i].to,jishu+1,max(zhi,jishu+1));
    41             else
    42                 dfs(N[i].to,0,zhi);
    43         }
    44     }
    45 }
    46 int main()
    47 {
    48     scanf("%d %d",&n,&m);
    49     nedge=0;
    50     memset(pre,0,sizeof(pre));
    51     memset(used,0,sizeof(used));
    52     memset(vis,0,sizeof(vis));
    53     for(int i=1;i<=n;i++)
    54         scanf("%d",&have[i]);
    55     for(int i=1;i<n;i++)
    56     {
    57        scanf("%d %d",&a,&b);
    58         add(b,a,0);
    59         vis[b]++;
    60         add(a,b,0);
    61         vis[a]++;
    62     }
    63     used[1]=1;
    64     if(have[1])
    65     dfs(1,1,1);
    66     else
    67     dfs(1,0,0);
    68     cout<<ans<<endl;
    69     return 0;
    70 }
  • 相关阅读:
    配置VSFTP文件服务器
    Apache Tomcat服务部署网站
    NFS实现Unix-like文件共享
    OpenSSH 服务配置与管理
    Redis 数据库配置与应用
    Rsync+Inotify 实现数据同步
    PXE+Kickstart 自动化部署系统
    PostFix+Dovecot 部署邮件系统
    部署MooseFS分布式文件系统
    MVC5+EF6 完整教程
  • 原文地址:https://www.cnblogs.com/hsd-/p/5668419.html
Copyright © 2020-2023  润新知