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 Specification:
Each input file contains one test case. Each case starts with a line containing 0, the number of nodes in a tree, and M (<), 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
.
The input ends with N being 0. That case must NOT be processed.
Output Specification:
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
题目分析 :第一行给了你2个数字,一个代表的是总节点数,一个代表的是叶子节点数 之后的几行 父亲节点与子节点,要求算出每一层叶子节点的数量
参考了别人的答案https://blog.csdn.net/qq_37613112/article/details/90577948
就是先将所有节点都先录入,然后对所有节点遍历,当它父亲节点的层数确定好后,它自己的层数也就能确定了
#include<iostream>
#include<string>
#include<stdlib.h>
#include<vector>
#define MaxNum 101
using namespace std;
typedef struct Node
{
int child = ;
int level = -;
int father;
}Tree[MaxNum]; int main()
{
Tree tree;
int n, m;
cin >> n>> m;
for (int k = ; k < m; k++)
{
int id,c,idj;
cin >> id >> c;
for (int j = ; j < c; j++)
{
cin >> idj;
tree[id].child++;
tree[idj].father = id;
}
}
tree[].father = ;
tree[].level = ;
if (n == )
{
cout << "" << endl;
return ;
}
int flag = ;
while (flag)
{
flag = ;
for (int i = ; i <=n; i++)
{
if (tree[tree[i].father].level != - && tree[i].level == -)tree[i].level = tree[tree[i].father].level + ;
else if (tree[tree[i].father].level == -)
flag = ;
}
}
int Level[MaxNum] = { };
int MaxLevel = -;
for (int i =; i <=n; i++)
{
if (tree[i].child== )Level[tree[i].level]++;
MaxLevel = MaxLevel > tree[i].level ? MaxLevel : tree[i].level;
}
cout << Level[];
for (int i = ; i <= MaxLevel; i++)
cout << " " << Level[i];
return ;
}
1004 Counting Leaves (30 分)的更多相关文章
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- 《JavaScript 模式》读书笔记(1)— 简介
哇,看了自己最近的一篇文章,其实那时候刚接触Jest,啥也不会(虽然现在其实也一样不会,嘿嘿),就像记录下工作中遇到的一些问题,其实,后来的一些发现吧,那两篇文章写的其实是有一些问题的.希望不会给大家 ...
- 遍历Map的四种方式(Java)
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- 鸟哥的Linux私房菜基础学习篇(第三版)——阅读笔记(二)
第一章 Linux是什么 1.Linux是什么 一套操作系统 早期的Linux是针对386开发的 具有可移植性 2.Unix及Linux的发展史 1973年,Unix诞生,Ritchie等人以C语言写 ...
- oracle12c数据库第一周小测验
一.单选题(共4题,30.4分) 1 ( )是位于用户与操作系统之间的一层数据管理软件.数据库在建立.使用和维护时由其统一管理.统一控制. A. A.DBMS B. B.DB C. C.DBS ...
- 在 centos6 上安装 LAMP
LAMP 代表的是 Linux, Apache, MySQL, 以及 PHP. 第一步,安装 Apache 使用 yum 安装 sudo yum install httpd 启动 httpd 服务 ...
- 搭建私有 Nuget 服务器教程(1)
对于 .NET 开发者来说,nuget 是必不可少的程序包管理工具.相应地,大部分开发团队都需要在内部搭建 Nuget 服务器,以管理私有 nupkg 包.本教程所使用的 Nuget 服务器,不是微软 ...
- 《Java8 Stream编码实战》正式推出
当我第一次在项目代码中看到Stream流的时候,心里不由得骂了一句"傻X"炫什么技.当我开始尝试在代码中使用Stream时,不由得感叹真香. 记得以前有朋友聊天说,他在代码中用了 ...
- qt creator源码全方面分析(3-5)
目录 qtcreatorlibrary.pri 使用实例 上半部 下半部 结果 qtcreatorlibrary.pri 上一章节,我们介绍了src.pro,这里乘此机会,把src目录下的所有项目文件 ...
- MySQL笔记(5)-- SQL执行流程,MySQL体系结构
MySQL的体系结构,可以清楚地看到 SQL 语句在 MySQL 的各个功能模块中的执行过程:Server层包括连接层.查询缓存.分析器.优化器.执行器等,涵盖MySQL的大多数核心服务功能,以及所有 ...
- 学习笔记----C语言的面向对象
2020-03-26 21:27:17 面向对象的编程语言都有一个类的概念,像Java.python等.类是对特定数据的特定操作的集合体.它包含两个范畴:数据和操作.C语言是没有类的概念的,但是 ...