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记录每 ...
随机推荐
- 浏览器URL传参最大长度问题
这几天为解决一个BUG头疼了一段时间,BUG现象如下: 一个选择人员的选择控件,当选择多个人时(50多个的时候),返回没有错误现象,而再一次打开的时候就报404错误.看到这个错误非常纳闷,无法下手,只 ...
- 第四章:更多的bash shell命令
第四章:更多的bash shell命令 监测程序 ps (其他ps内容见#1 ) Unix风格的ps命令参数 参数 描述 -A 显示所有进程 -N 显示与指定参数不符的所有进程 -a 显示除控制进程( ...
- bzoj 1391 [Ceoi2008]order(最小割)
[题意] 有n个有偿工作选做,m个机器,完成一个工作需要若干个工序,完成每个工序需要一个机器,对于一个机器,在不同的工序有不同的租费,但买下来的费用只有一个.问最大获益. [思路] 对于工作和机器建点 ...
- 内核源码分析之进程调度机制(基于3.16-rc4)
进程调度所使用到的数据结构: 1.就绪队列 内核为每一个cpu创建一个进程就绪队列,该队列上的进程均由该cpu执行,代码如下(kernel/sched/core.c). DEFINE_PER_CPU_ ...
- Bmob第三方登录详解
Bmob第三方登录详解 Bmob 第三方登录 简介 本文主要介绍新浪微博,QQ,微信的登录接入以及如何配合BmobSDK中的第三方登录功能实现第三方登录. 在使用之前请先按照快速入门创建好可以调用Bm ...
- Codeforces 381 简要题解
做的太糟糕了...第一题看成两人都取最优策略,写了个n^2的dp,还好pre-test良心(感觉TC和CF的pretest还是很靠谱的),让我反复过不去,仔细看题原来是取两边最大的啊!!!前30分钟就 ...
- sonar 代码质量管理平台
1) 下载 从sonar官网http://www.sonarsource.org/下载 (版本当然是最新的了) 在官网上是不分系统的,一个zip包,下下来之后,包里包含 windows .l ...
- 第二百九十、一、二天 how can I 坚持
上周日,腊八,好冷,去菏泽了.坐火车,没座.好冷,下午菏泽学院,大体看了看,很是怀念,有点遗憾,在网吧玩的时间太长了,留不住. 周一,长太婚礼,竟然没有只是吃了个饭,有点小失望,下午坐车回京,两点坐的 ...
- Spark SQL概念学习系列之为什么使用 Spark SQL?(二)
简单地说,Shark 的下一代技术 是Spark SQL. 由于 Shark 底层依赖于 Hive,这个架构的优势是对传统 Hive 用户可以将 Shark 无缝集成进现有系统运行查询负载. 但是也看 ...
- 关于Js脚本的延迟执行
关于标签,我们有一般会放在两个地方,一个是在标签的内部,另一个是放在前面.这一次我们着重讲解一下放在标签内部.在我们用Js操作DOM元素时,如果不延时执行,DOM元素其实是根本还没有被渲染出来的. ...