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 分)的更多相关文章

  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. 李宏毅深度学习与人类语言处理-introduction

    深度学习与人类语言处理(Deep learning for Human Language Processing) 李宏毅老师深度学习与人类语言处理课程笔记,请看正文 这门课会学到什么? 为什么叫人类语 ...

  2. element多层导航菜单

    很久没写博客了原因就是懒, 刚下班今天写了个基于element导航菜单实现多层菜单(可以无限多层) 组件核心思想就是组件递归(这个有时候面试会问到) 然后就判断children有没有内容,没内容就是选 ...

  3. LSP原则—关于正方形不是长方形

    长方形有二个属性长和宽.并有一个设置长的方法和设置宽的方法,还有一个求面积的方法. 像下面 private int length; private int width; public void set ...

  4. OpenMP Programming

    一.OpenMP概述 1.OpenMP应用编程接口API是在共享存储体系结构上的一个编程模型 2.包含 编译制导(compiler directive).运行库例程(runtime library). ...

  5. C语言程序设计(十三) 文件操作

    第十三章 文件操作 文本文件:将数值型数据的每一位数字作为一个字符以其ASCII码的形式存储(每一位数字都单独占用一个字节的存储空间) 二进制文件:数据值是以二进制形式存储的 文本文件可以方便地被其他 ...

  6. redis实现数据库(一)

    转:https://www.cnblogs.com/beiluowuzheng/p/9738159.html 服务器中的数据库 Redis服务器将所有数据库都保存在服务器状态redis.h/redis ...

  7. js 模拟鼠标绘制方块

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Python3学习之路~9.4 队列、生产者消费者模型

    一 队列queue 当必须在多个线程之间安全地交换信息时,队列在线程编程中特别有用. 队列的作用:1.解耦,使程序直接实现松耦合 2.提高处理效率 列表与队列都是有顺序的,但是他们之间有一个很大的区别 ...

  9. 简易版cnlog

    目录 简易版cnlog html文件 mycess.cess 页面效果 简易版cnlog 注意的点 1.一定先分好块标签,再基于一个个块标签进行装饰(相当于给一个个人化妆) 2.浮动: 我的理解是从一 ...

  10. 【webpack 系列】基础篇

    Webpack 基础篇 基本概念 Webpack 是一个现代 JavaScript 应用程序的静态模块打包器.当 webpack 处理应用程序时,它会递归地构建一个依赖关系图,其中包含应用程序需要的每 ...