zoukankan      html  css  js  c++  java
  • PAT 1004

    1004. 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 

    使用层次遍历来统计每一层的叶子结点数量,定义了一个队列,并用变量pos来表示每一层的最后一个结点在队列中的位置。

    代码

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     int N,M;
     7     int Tree[100][100];
     8     int Queue[100];
     9     int s,e,leaf_num,pos,level;
    10     int ID;
    11     int i,j;
    12     while(scanf("%d%d",&N,&M) != EOF){
    13         memset(Tree,0,sizeof(Tree));
    14         for (i=0;i<M;++i){
    15             scanf("%d",&ID);
    16             scanf("%d",&Tree[ID][0]);
    17             for(j=1;j<=Tree[ID][0];++j){
    18                 scanf("%d",&Tree[ID][j]);
    19             }
    20         }
    21         s = 0;
    22         e = 0;
    23         Queue[e++] = 1;
    24         pos = e;
    25         leaf_num = 0;
    26         level = 1;
    27         while(s != e){
    28             int x = Queue[s++];
    29             if (Tree[x][0] == 0)
    30                 ++leaf_num;
    31             else{
    32                 for(i=1;i<=Tree[x][0];++i){
    33                     Queue[e++] = Tree[x][i];
    34                 }
    35             }
    36             if(s == pos){
    37                 if (level == 1)
    38                     printf("%d",leaf_num);
    39                 else
    40                     printf(" %d",leaf_num);
    41                 ++level;
    42                 leaf_num = 0;
    43                 pos = e;
    44             }
    45         }
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    用户管理
    网线制作与分类
    5.虚函数,覆盖,多态,异常处理
    4.类的继承
    3.运算符重载
    7.STL
    6.泛型编程与模板
    C++中>>,<<的重载问题
    2.名字空间和构造函数
    1.C和C++的区别
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1004.html
Copyright © 2011-2022 走看看