zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    atoi (String to Integer) leetcode
    按层逆遍历一棵树,使用满二叉树存储
    unix网络编程-配置unp.h头文件
    ListView系列(七)——Adapter内的onItemClick监听器四个arg参数
    Windows系统下安装VirtualBox,系统找不到指定路径的做法
    Android Fragment完全解析,关于碎片你所需知道的一切
    【Android开源框架列表】
    fragment报错
    2013 年开源中国 10 大热门 Java 开源项目
    【移动开发】Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)
  • 原文地址:https://www.cnblogs.com/hsd-/p/5668419.html
Copyright © 2011-2022 走看看