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. 记:Tmall活动页面开发

    一.年轻的我 “无人不成商”,如果一个电子商务网站想要做起来,搞活动时必不可少的(引入流量.提高用户黏度.活跃网站氛围),今天打折,明天送红包. (立秋活动,右) 作为一个前端,我当然要从技术的角度来 ...

  2. 教你利用iframe在网页中显示天气

    来源:http://www.ido321.com/921.html css: 1: *{margin:0;padding:0;list-style-type:none;} 2: a,img{borde ...

  3. Buffer -nodejs

    纯 JavaScript 对 Unicode 友好但是无法很好地处理二进制数据.当我们面对类似 TCP 流或文件系统时,是需要处理八位流的.Node 有几种操作.创建以及消费八位流的策略.原始数据保存 ...

  4. The First Pig Task

                         The First Pig Program 环境: Hadoop-1.1.2 pig-0.11.1 linux系统为CentOS6.4 jdk1.6 在伪分布 ...

  5. ubuntu下通过pip安装pyside

    首先安装相关库 sudo apt-get install build-essential git cmake libqt4-dev libphonon-dev python2.7-dev libxml ...

  6. Linux下tar.xz结尾的文件的解压方法

    $xz -d ***.tar.xz $tar -xvf  ***.tar 可以看到这个压缩包也是两层压缩,外面是xz压缩方式,里层是tar压缩方式.

  7. Visual Studio 2013智能提示失效解决办法

    各种解决VS2013智能提示失效办法: 1.重置所有设置    工具->导入导出设置->重置所有设置 2.智能提示开关: 工具->选项->文本编辑器->C#->常规 ...

  8. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  9. Winter is coming Just have a little faith. JSF框架简介与实例

    JSF 体系结构: JSF 的主要优势之一就是它既是Java Web应用程序的用户界面标准又是严格遵循模型-视图-控制器 (MVC) 设计模式的框架.用户界面代码(视图)与应用程序数据和逻辑(模型)的 ...

  10. Spring EL Lists, Maps example

    In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...