PAT 1004
1004. Counting Leaves (30)
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的更多相关文章
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT 1004 成绩排名 (20)(代码)
1004 成绩排名 (20)(20 分) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为\ 第1行:正整数n 第2行:第1 ...
- PAT——1004. 成绩排名
原题目:https://www.patest.cn/contests/pat-b-practise/1004 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每 ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004. 成绩排名 (20)
读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...
- PAT 1004. 成绩排名 (20) JAVA
读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...
- 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 ...
- PAT 1004 成绩排名
https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448 读入n名学生的姓名.学号.成绩,分别输出成绩 ...
- pat -1004(树的遍历)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 思路: (1)用vector记录每 ...
随机推荐
- 通过gdb跟踪进程调度分析进程切换的过程
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本实验目的:通过gdb在lin ...
- Oracle创建用户及表空间 代码片段
create tablespace testdatalogging datafile 'D:\oracle\oradata\orcl\testdata.dbf' size 50m autoextend ...
- 快速开发 jQuery 插件的 10 大技巧(转)
转自:http://www.oschina.net/news/41776/jquery-10-tips 在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模 ...
- Windows Azure中的配置管理
最近一直在做项目迁移的工作,由传统的ASP.NET转到Windows Azure,这里介绍一下Azure的配置管理.在传统的WinForm或ASP.NET项目下,配置文件为web.config(app ...
- es基础操作
在curl 的 url 中 , 问号后台可以加上pretty=true , 可以将返回来的json进行格式化 . 如果es集群中只有一个node , 那么他的集群健康状态是黄色的 , 只需要再加一个n ...
- Nodejs与Net 和SQL 交互利器Edge.js
https://github.com/tjanczuk http://www.cnblogs.com/joylee/archive/2013/02/05/msnodesql.html edge.js这 ...
- public, protected, private, internal, protected internal简析
public是可访问权限最高的,比如姓名,每个人都可以知道别人的姓名,这个不是什么秘密 protected的访问权限要低些,只有子类才可以访问得到父类的protected属性.就好像老子的财产只有儿子 ...
- 第二百六十六天 how can I 坚持
有些小郁闷,上班没事干,技术.要买房,还要结婚,买了房哪还有彩礼钱,还得借钱,愁人. 该咋办,毕梦琪. 哎.今天还和程秀通说好的去看房呢,整天犹豫不定的.烦. 今天平安夜啊.吃了个苹果,一生平平安安, ...
- Struts2运行流程分析
一.Struts2运行流程图: 二.运行流程分析: 1. 请求发送给StrutsPrepareAndExecuteFilter 2.StrutsPrepareAndExecuteFilter询问Act ...
- hadoop streaming 编程
概况 Hadoop Streaming 是一个工具, 代替编写Java的实现类,而利用可执行程序来完成map-reduce过程.一个最简单的程序 $HADOOP_HOME/bin/hadoop jar ...