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:

  1. 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:

  1. 2 1
  2. 01 1 02

Sample Output:

  1. 0 1
  2.  
  3. 题目分析 :第一行给了你2个数字,一个代表的是总节点数,一个代表的是叶子节点数 之后的几行 父亲节点与子节点,要求算出每一层叶子节点的数量
    参考了别人的答案https://blog.csdn.net/qq_37613112/article/details/90577948
    就是先将所有节点都先录入,然后对所有节点遍历,当它父亲节点的层数确定好后,它自己的层数也就能确定了
  1. #include<iostream>
  2. #include<string>
  3. #include<stdlib.h>
  4. #include<vector>
  5. #define MaxNum 101
  6. using namespace std;
  7. typedef struct Node
  8. {
  9. int child = ;
  10. int level = -;
  11. int father;
  12. }Tree[MaxNum];
  13.  
  14. int main()
  15. {
  16. Tree tree;
  17. int n, m;
  18. cin >> n>> m;
  19. for (int k = ; k < m; k++)
  20. {
  21. int id,c,idj;
  22. cin >> id >> c;
  23. for (int j = ; j < c; j++)
  24. {
  25. cin >> idj;
  26. tree[id].child++;
  27. tree[idj].father = id;
  28. }
  29. }
  30. tree[].father = ;
  31. tree[].level = ;
  32. if (n == )
  33. {
  34. cout << "" << endl;
  35. return ;
  36. }
  37. int flag = ;
  38. while (flag)
  39. {
  40. flag = ;
  41. for (int i = ; i <=n; i++)
  42. {
  43. if (tree[tree[i].father].level != - && tree[i].level == -)tree[i].level = tree[tree[i].father].level + ;
  44. else if (tree[tree[i].father].level == -)
  45. flag = ;
  46. }
  47. }
  48. int Level[MaxNum] = { };
  49. int MaxLevel = -;
  50. for (int i =; i <=n; i++)
  51. {
  52. if (tree[i].child== )Level[tree[i].level]++;
  53. MaxLevel = MaxLevel > tree[i].level ? MaxLevel : tree[i].level;
  54. }
  55. cout << Level[];
  56. for (int i = ; i <= MaxLevel; i++)
  57. cout << " " << Level[i];
  58. return ;
  59. }

1004 Counting Leaves (30 分)的更多相关文章

  1. PAT 1004 Counting Leaves (30分)

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

  2. 1004 Counting Leaves (30分) DFS

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

  3. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

    题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...

  4. 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 ...

  5. 1004. Counting Leaves (30)

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

  6. PAT 解题报告 1004. Counting Leaves (30)

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

  7. PAT 1004. Counting Leaves (30)

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

  8. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  9. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

随机推荐

  1. SpringBoot2 整合ElasticJob框架,定制化管理流程

    本文源码:GitHub·点这里 || GitEE·点这里 一.ElasticJob简介 1.定时任务 在前面的文章中,说过QuartJob这个定时任务,被广泛应用的定时任务标准.但Quartz核心点在 ...

  2. Redis系列六 - 浅谈如何设计秒杀系统

    前言 设计一个系统之前,我们肯定要先确认系统业务场景是怎样的,下面就以某电商平台上的秒杀活动为场景,一起来探讨一个秒杀系统改如何去设计. 场景 我们现在要卖100件纸尿布,按照系统的用户量及以往经验来 ...

  3. Redux 架构理解

    Redux 是一种前端“架构模式”,是 Flux 架构的一种变种,用来提供可预测的状态管理.虽然经常和 React 一起被提及,但是 Redux 却不仅仅只能用于 React,还可以将其运用到其他前端 ...

  4. scrapy启动

    创建项目 在开始爬取之前,您必须创建一个新的Scrapy项目. 进入您打算存储代码的目录中,运行下列命令: scrapy startproject scrapytest 第一种scrapy gensp ...

  5. spring多数据源分布式事务的分析与解决方案

    一.概述 1.业务背景 对老系统进行重构合并,导致新系统需要同时对3个数据库进行管理.由于出现跨库业务,需要实现分布式事务. 2.开发环境 spring框架版本  4.3.10.RELEASE 持久层 ...

  6. JAVA反射概念及使用详解(超详细)

    JAVA反射概念及使用详解 一.什么是反射? 反射:框架设计的灵魂 框架:半成品软件.可以在框架的基础上进行软件开发,简化编码 反射:将类的各个组成部分封装为其他对象,这就是反射机制 ​ 好处: ​ ...

  7. .tar.xz文件的创建和解压

    创建tar.xz文件:只要先 tar cvf xxx.tar xxx/ 这样创建xxx.tar文件先,然后使用 xz -z xxx.tar 来将 xxx.tar压缩成为 xxx.tar.xz 解压ta ...

  8. apply 和 call 方法详解【转载】

    本文转载至:http://blog.csdn.net/business122/article/details/8000676 我在一开始看到javascript的函数apply和call时,非常的模糊 ...

  9. 【Weiss】【第03章】练习3.17:懒惰删除

    [练习3.17] 不同于我们已经给出的删除方法,另一种是使用懒惰删除的方法. 为了删除一个元素,我们只标记上该元素被删除的信息(使用一个附加的位域). 表中被删除和非被删除的元素个数作为数据结构的一部 ...

  10. IOptions、IOptionsMonitor以及IOptionsSnapshot

    背景 ASP.NET Core引入了Options模式,使用类来表示相关的设置组.简单的来说,就是用强类型的类来表达配置项,这带来了很多好处.初学者会发现这个框架有3个主要的面向消费者的接口:IOpt ...