zoukankan      html  css  js  c++  java
  • A1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

    Input

    Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

    ID K ID[1] ID[2] ... ID[K]
    

    where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

    Output

    For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

    The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.

    Sample Input

    2 1
    01 1 02
    

    Sample Output

    0 1
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <string>
     8 #include <stack> 
     9 #include <queue>
    10 using namespace std;
    11 const int maxn=110; 
    12 int n,m;
    13 vector<int> tree[maxn];//
    14 int lost[maxn]={0},maxdep=0; 
    15 
    16 void DFS(int root,int depth)
    17  {
    18      if(depth>maxdep)maxdep=depth;
    19      if(tree[root].size()==0)//为叶子节点 
    20      {
    21          lost[depth]++; 
    22          return ;
    23      }
    24      for(int i=0;i<tree[root].size();i++)
    25      {
    26          DFS(tree[root][i],depth+1);
    27      }
    28     
    29  }
    30 int main(){
    31     scanf("%d %d",&n,&m);
    32     if(n==0){
    33     printf("0");
    34     return 0; 
    35     }
    36     for(int i=0;i<m;i++)
    37     {
    38         int id,count; 
    39         scanf("%d %d",&id,&count);
    40         for(int j=0;j<count;j++)
    41         {
    42             int tmp;
    43             scanf("%d",&tmp);
    44             tree[id-1].push_back(tmp-1); 
    45         }
    46          
    47     }
    48     //遍历树
    49      DFS(0,0); 
    50      printf("%d",lost[0]); 
    51      for(int i=1;i<=maxdep;i++)printf(" %d",lost[i]);
    52     return 0;
    53 }
  • 相关阅读:
    羊年春节微信数据大解析
    微信公众号推荐(自己关注的微信公众平台导航)
    微信要革"传统电视"的命吗?
    微信是在学苹果模式吗?
    发微信红包啦!借花献佛
    有原创保护能力的公众帐号可申请页面模版功能和图文消息正文插入历史群发链接
    编程能力与编程年龄
    代码执行的效率
    对九个超级程序员的采访
    编程真难啊
  • 原文地址:https://www.cnblogs.com/ligen/p/4321244.html
Copyright © 2011-2022 走看看