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. 05 mapreduce快速入门

    统计HDFS的/wordcount/input/a.txt文件中的每个单词出现的次数——wordcount package cn.oracle.core; import java.io.IOExcep ...

  2. selenium基本对象之——数值型

    python的数值类型,除了魔法方法以为,只有下面的这些方法: 整形的方法有:as_integer_ratio.bit_length.from_bytes.to_bytes.conjugate.ima ...

  3. Array.isArray() 判断是不是数组

    var arr = new Array(); if(Array.isArray()){ console.log('yes') } else { conssole.log('no') }

  4. go源码分析(四) net包获取主机ip 子网掩码相关分析

    获取本地的ip时 顺便学习了下标准库net中的实现 在net/interface.go中进行了入口调用,返回值为Addr的slice func InterfaceAddrs() ([]Addr, er ...

  5. javaScript 基础知识汇总(八)

    1.Map Set WeakMap 和WeakSet Map 是一个键值对的集合,主要的方法包括: new Map() 创建Map map.set(key,value)  根据键(key)存储值(va ...

  6. 《ASP.NET Core 3框架揭秘》5折预售[发布试读章节]

    <ASP.NET Core 3框架揭秘>于昨天在下午京东正式开始预售,并在半天之内销售近一千套.为了回馈读者,出版社与京东谈了一个5折的价格,这是一个连我都没有想到的价格,至少我写着几本书 ...

  7. angular 中嵌套 iframe 报错

    错误如下 Error: unsafe value used in a resource URL context at DomSanitizationServiceImpl.sanitize... 解决 ...

  8. CF33C Wonderful Randomized Sum 题解

    原题链接 简要题意: 你可以无限次的把该数组的一个前缀和后缀 \(\times -1\),问最终的最大序列和. 这题盲目WA了数次才知道本质 这题89个数据吊打std CF真好啊,发现一个错后面就不测 ...

  9. leetcode之820. 单词的压缩编码 | python极简实现字典树

    题目 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", "bell& ...

  10. varchar int 查询 到底什么情况下走索引?

    一个字符类型的.一个int类型的,查询的时候到底会不会走索引,其实很多工作了几年的开发人员有时也会晕,下面就用具体事例来测试一下. 1.  准备工作 先准备2张表,以备后续测试使用. 表1:创建表te ...