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 }
  • 相关阅读:
    CC2431 代码分析⑦
    CC2431 代码分析 ⑤
    CC2431 代码分析⑥
    CC2431 代码分析④-衣锦还乡的CC2431
    基于CC2530/CC2430 的光强采集系统--ADC实验
    Server2012R2 ADFS3.0 The same client browser session has made '6' requests in the last '13'seconds
    Dynamics CRM2013 任务列表添加自定义按钮
    Dynamics CRM 2011/2013 section的隐藏
    Dynamics CRM2013 定制你的系统登录后的首页面
    Dynamics CRM EntityCollection 根据实体中的某个字段为依据去除重复数据
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1004.html
Copyright © 2011-2022 走看看