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[][];
 8     int Queue[];
 9     int s,e,leaf_num,pos,level;
     int ID;
     int i,j;
     while(scanf("%d%d",&N,&M) != EOF){
         memset(Tree,,sizeof(Tree));
         for (i=;i<M;++i){
             scanf("%d",&ID);
             scanf("%d",&Tree[ID][]);
             for(j=;j<=Tree[ID][];++j){
                 scanf("%d",&Tree[ID][j]);
             }
         }
         s = ;
         e = ;
         Queue[e++] = ;
         pos = e;
         leaf_num = ;
         level = ;
         while(s != e){
             int x = Queue[s++];
             if (Tree[x][] == )
                 ++leaf_num;
             else{
                 for(i=;i<=Tree[x][];++i){
                     Queue[e++] = Tree[x][i];
                 }
             }
             if(s == pos){
                 if (level == )
                     printf("%d",leaf_num);
                 else
                     printf(" %d",leaf_num);
                 ++level;
                 leaf_num = ;
                 pos = e;
             }
         }
     }
     return ;
 }

PAT 1004的更多相关文章

  1. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  2. PAT 1004 成绩排名 (20)(代码)

    1004 成绩排名 (20)(20 分) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为\ 第1行:正整数n 第2行:第1 ...

  3. PAT——1004. 成绩排名

    原题目:https://www.patest.cn/contests/pat-b-practise/1004 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每 ...

  4. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  5. PAT 1004. 成绩排名 (20)

    读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...

  6. PAT 1004. 成绩排名 (20) JAVA

    读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...

  7. PAT 1004 To Fill or Not to Fill (25)

    题目描写叙述 With highways available, driving a car from Hangzhou to any other city is easy. But since the ...

  8. PAT 1004 成绩排名

    https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448 读入n名学生的姓名.学号.成绩,分别输出成绩 ...

  9. pat -1004(树的遍历)

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 思路: (1)用vector记录每 ...

随机推荐

  1. 《Python 学习手册4th》 第四章 介绍Python对象类型

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容(每天看42页内容,可以保证月底看完此书) ''' ...

  2. 细雨学习笔记:Jmeter参数化

    目前我用到两种方式: 1)某个参数,值不常改变的,好多地方都用到:请用“用户定义的变量” 用户组,右键--添加--配置原件--用户定义的变量,在这添加. 如何使用呢?在需要用到此参数的地方这样引用: ...

  3. 2015NOIP简单说说

    在机房度过最后两节课然后滚回去赶文化课,准备期中考试,高考.AFO的称号毫无悬念的归来了.DAY1T2的失误不能拿下230,只能190滚粗,DAY2一上午都在混沌.旁边的哥们求我给看第一题,于是他就对 ...

  4. 利用Javascript获取当前日期的农历日期

    来源:http://www.ido321.com/926.html JavaScript代码 1: /*设置农历日期*/ 2: var CalendarData=new Array(100); 3: ...

  5. bzoj 3218 a + b Problem(最小割+主席树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3218 [题意] 给n个格子涂白或黑色,白则wi,黑则bi的好看度,若黑格i存在: 1& ...

  6. Genesis-3D开源游戏引擎简介!

    Genesis-3D由搜狐畅游公司超百人引擎研发团队历时数年耗费巨资自主研发,是国内外首款商业开源的3D游戏引擎平台.它包括跨平台渲染引擎.2D引擎.物理引擎.音效系统.粒子系统.动画系统.服务器引擎 ...

  7. ACM竞赛 Java编程小结

    1.字符串的长度 String str = new String(" abcd"); int length = str.length(); 2.数组的长度.排序 2.1对于 a[] ...

  8. 第三百零六天 how can I 坚持

    今天做了件并不是我风格的事,送了张公交卡,还没送出去,好难搞啊.这天会铭记的.如果将来我们能走在一起. 中午去朝阳门拿了我的荣事达破壁机,好大啊,怎么带回家啊,还有,回家要不要买两只烤鸭啊. 今天聊了 ...

  9. Apache Spark BlinkDB

    BlinkDB是一个用于在海量数据上进行交互式SQL的近似查询引擎. 它允许用户通过在查询准确性和查询响应时间之间做出权衡,完成近似查询. 其数据的精度被控制在允许的误差范围内. 为了达到这个目标,B ...

  10. JNI调用测试

    有需求使用JNI调用,籍着这个机会按照<Linux下测试Java的JNI(Java Native Interface)>上进行了下测试. 这篇文章记录得很清楚了,对原理未做深入的分析,希望 ...