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 }
  • 相关阅读:
    基于Linux平台的自动化运维Devops-----之自动化系统部署
    Centos7.1 mini版安装后安装图形界面教程
    python包管理之Pip安装及使用-1
    maven中jar、war、pom的区别
    黄焖鸡
    django-celery配置
    文档编写注意事项
    java时间处理,获取当前时间的小时,天,本周周几,本周周一的日期,本月一号的日期
    flink连接hbase方法及遇到的问题
    pycharm远程debug(内网环境,跳板机)
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1004.html
Copyright © 2011-2022 走看看