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 01level, 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

即遍历整颗数,使用DFS或者DFS
用数组记录每个节点的子节点是谁
 #include <iostream>
#include <vector>
#include <queue> using namespace std; //给出一棵树,问每一层的叶子结点数量
//使用BFS或者DFS vector<vector<int>>nodes();
vector<int>depth();
int maxDepth = -; void DFS(int index, int h)
{
maxDepth = maxDepth > h ? maxDepth : h;
if (nodes[index].size() == )//data[index].size() == 0)//即为叶子结点
depth[h]++;//层数 for (int i = ; i < nodes[index].size(); ++i)
DFS(nodes[index][i], h + );
} void BFS( )
{
queue<int>q;
q.push();
vector<int>level(, );//记录节点层数
while (!q.empty())
{
int index = q.front();
q.pop();
maxDepth = maxDepth > level[index] ? maxDepth : level[index];//存储最大的层数
if (nodes[index].size() == )//此节点为叶子节点
depth[level[index]]++;//之所以要记录每个节点的层数,是因为,同一层有多个节点
for (int i = ; i < nodes[index].size(); ++i)
{
level[nodes[index][i]] = level[index] + ;//孩子结点层数比父节点多一层
q.push(nodes[index][i]);//将其孩子全部存入
}
}
} int main()
{
int N, M;//N为节点数目
cin >> N >> M;
for (int i = ; i < M; ++i)
{
int ID, k, a;
cin >> ID >> k;
for (int j = ; j < k; ++j)
{
cin >> a;
nodes[ID].push_back(a);//即为一个节点底下所挂的子节点
}
} //DFS(1,0);
BFS( );
cout << depth[];
for (int i = ; i <= maxDepth; ++i)
cout << " " << depth[i];
cout << endl; return ; }

PAT甲级——A1004 Counting Leaves的更多相关文章

  1. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  2. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  3. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...

  4. PAT甲1004 Counting Leaves【dfs】

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

  5. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  6. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

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

  7. pat 甲级 1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...

  8. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

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

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

随机推荐

  1. Neo4j中實現自定義中文全文索引

    資料庫檢索效率時,一般首要優化途徑是從索引入手,然後根據需求再考慮更復雜的負載均衡.讀寫分離和分散式水平/垂直分庫/表等手段:索引通過資訊冗餘來提高檢索效率,其以空間換時間並會降低資料寫入的效率,因此 ...

  2. 5步减小你的CSS样式表

    第一步:学会如何组合选择器 什么是选择器?如果你还不知道什么叫做”选择器”,你可以先参考一下w3schools.com的CSS语法概述. 未优化的CSS代码在下面的图例中,你会看到来自三个不同选择器的 ...

  3. java oop第07章_集合框架

    一. 什么是集合: 在Java中提供了一些可以保存同一数据类型的数据集称为集合,就是规定了一些集合的规范(接口.抽象类.实现类)及方法, 方便我们程序在保存数据时进行增.删.改.查操作,编程更加高效. ...

  4. 和Excel函数date同样功能的VBA函数DateSerial用法

    Sub 日期别()On Error Resume Nextlastrow = Sheets("运营日报").Range("a1048576").End(xlUp ...

  5. duilib教程之duilib入门简明教程17.事件处理和消息响应

    界面的显示方面就都讲完啦,下面来介绍下控件的响应.    前面的教程只讲了按钮和Tab的响应,即在Notify函数里处理.其实duilib还提供了另外一种响应的方法,即消息映射DUI_BEGIN_ME ...

  6. LUOGU P1402 酒店之王 (网络流)

    解题思路 应该比较显然得能看出这是个网络流,将$S$与房间连边,房间与人连边,人与菜连边,菜与汇点连边,边的流量均为1.但这样是错误的,因为有可能一个人跑过去2的流量,所以要将人拆点限流. #incl ...

  7. range()函数在python3与python2中的区别

    range()函数在python3与python2中的区别 - CSDN博客 https://blog.csdn.net/weixin_37579123/article/details/8098038 ...

  8. Spring+SpringMVC+Mybatis搭建的网站的处理流程总结

    最近学习了如何使用SSM框架搭建网站,以前没用过框架,第一次使用,总结一下自己对框架处理流程的理解

  9. 建立ftp服务器和客户端

    参考:https://www.cnblogs.com/judes/p/9546447.html 补充: 权限设置:如下所示,如果需要上传文件需要勾选write权限,需要在文件中添加内容勾选append ...

  10. JAVA_ArrayList和HashSet

    ArrayList ArrayList在Java中主要是以线性表的形式进行存储,其本质是数组,不过相对于数组的长度不可变这一缺点,其实行了长度可变策略,使你在使用时,感觉到其就是一个无限长度的数组,而 ...