• 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 }
  • 相关阅读:
    activemq学习
    shell变量
    ext3文件系统目录限制问题
    linux性能优化cpu 磁盘IO MEM
    vs2010下编译osip2和eXosip2的4.0.0版的静态库及搭建开发环境
    samba的rpm包,只有tar.gz文件安装
    随记
    mount/umount系统调用
    不定参数的传递VA_LIST的用法
    samba服务器源码安装(非rpm)
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1004.html
Copyright © 2020-2023  润新知