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. 分布式图数据库 Nebula Graph 的 Index 实践

    导读 索引是数据库系统中不可或缺的一个功能,数据库索引好比是书的目录,能加快数据库的查询速度,其实质是数据库管理系统中一个排序的数据结构.不同的数据库系统有不同的排序结构,目前常见的索引实现类型如 B ...

  2. django 从零开始 10 缓存控制

    设置django缓存,比如一些访问频繁的页面  首页  并且不需要进行太大的变化,那么可以设置为一定是时间内请求该url视图 不经过视图计算直接返回缓存的内容 django文档选择了几种方式,但是我这 ...

  3. scrapy的扩展件extensions

    extensions.py文件 # -*- coding: utf-8 -*- # 该扩展会在以下事件时记录一条日志: # spider被打开 # spider被关闭 # 爬取了特定数量的条目(ite ...

  4. git常规使用命令笔记

    使用git也有不短的时间,这篇文章主要默写自己在提交代码经常使用的一些密令,仅供参考并不是教程. git log 查看日志 git branch 查看分值 git branch Name 新建一个Na ...

  5. 测试必知必会系列- Linux常用命令 - cd

    21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 如何进 ...

  6. git版本控制系统小白教程(上)

    前言:本文主要介绍git版本控制系统的一些基础使用,适合小白入门,因为内容较多,会分为两部分进行分享. Git介绍 ​ Git是目前世界上最先进的分布式版本控制系统.并且它是一个开源的分布式版本控制系 ...

  7. ERP系统功能及部署方式

    ERP是对企业经营过程中的一些事项进行系统化管理的一种软件,所以ERP软件包含企业经营过程中需要用到的多种功能管理模块,并且ERP软件有两种部署方式.下面一起来了解一下相关的知识吧! ERP系统的功能 ...

  8. 【简说Python WEB】数据库

    目录 [简说Python WEB]数据库 数据库表 docker安装MySQL Flask-SQLAlchemy操纵MySQL数据库 初始化 定义模型 定义关系 数据库的CRUD操作 创建表 inse ...

  9. zabbix笔记_008 zabbix监控交换机路由器

    zabbix监控交换机路由器 要监控路由器交换机,需要使用到SNMP协议 SNMP是一个简单网络管理协议,他基于C/S模型实现的监控和管理. 服务器安装SNMP: yum -y install net ...

  10. 一夜搞懂 | JVM GC&内存分配

    前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习导图 一.为什么要学习GC&内存分配? 时代发展到现在,如今的内存动态分配与内存回收技术已经相当成 ...