PAT 1004 Counting Leaves (30分) dfs求每层叶子节点数

1004 Counting Leaves (30)(30 分)树的遍历 1 题目 2 解题思路 3 AC代码 1 题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one ... 阅读详情

题目

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<N<100, the number of nodes in a tree, and M (<N), 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

题目大意

翻译过来就是,有一棵树,给出了节点数N(根节点编号为01),非叶子节点数M,给出了每个非叶子节点的孩子个数 K,每个孩子的编号。

输出 每一层有几个叶子节点。

思路分析

  • 不用构建树的结构体,首先我们不用区分孩子的顺序,知道它有几个孩子,编号是几就可以了;其次,每个非叶子节点所拥有的孩子数目是不一样的,也无法构建结构体。
  • 所以,我们用一个vector数组,每个vector保存一个非叶子节点的所有孩子编号。
  • 判断是否是叶子节点很简单,只需要判断它对应的vector的size是否为0就可以。
  • 由于我们不能直接确定树的结构,而最后输出每一层的非叶子节点个数,所以需要一个变量maxLevel保存这个数有多高,也就是最深一层是哪一层。
  • 然后我们要求得每一层的叶子节点有几个,所以还得用一个数组保存每一层的叶子节点数。
  • 接下来,从根节点开始,进行递归就好了
	// 根节点编号是1,层级是0 
	helper(1, 0);
	// index 节点编号; level 节点在哪一层
	void helper(int index, int level) {
		// 他没有孩子,它是叶子节点,
		if (nodes[index].size() == 0) {
			// 他所在这一层叶子节点数+1 
			levelLeaves[level]++;
			// 更新 这棵树的最深的有效层
			maxLevel = max(maxLevel, level);
			return;
		}
		// 他的所有孩子都处于他的下一层 
		for (int i = 0; i < nodes[index].size(); ++i) 
			helper(nodes[index][i], level + 1);
	}
} 

完整代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 保存每个节点的全部孩子,自己的编号作为下标,值是一个vector 
vector<int> nodes[100];
// 保存每一层有几个叶子节点 
int levelLeaves[100];
// 题目最多有100个节点,保存最深的有效层是哪一层

int maxLevel = -1; 

//
void helper(int index, int level) {
	// 他没有孩子,他所在这一层叶子节点数+1 
	if (nodes[index].size() == 0) {
		levelLeaves[level]++;
		// 更新 最深的有效层
		maxLevel = max(maxLevel, level);
		return;
	}
	// 他的所有孩子都处于他的下一层 
	for (int i = 0; i < nodes[index].size(); ++i) 
		helper(nodes[index][i], level + 1);
} 

int main() {
	
	// n个节点,m个非叶子节点 
	int n, m;
	
	int index, kids, child;
	
	cin >> n >> m;
	
	// 每一行是一个非叶子节点的情况
	// 编号 几个孩子 孩子1编号 孩子2编号 ... 
	for (int i = 0; i < m; ++i) {
		cin >> index >> kids;
		for (int j = 0; j < kids; ++j) {
			cin >> child;
			nodes[index].push_back(child);
		}
	} 
	
	// 根节点编号是1,层级是0 
	helper(1, 0);
	
	// 这样写会导致最终多出来一个空格,不满足输出要求	
//	for (int i = 0; i < maxLevel; ++i)
//		cout << levelLeaves[i] << " ";
	// 输出每一层的叶子节点个数
	cout << levelLeaves[0];
	// maxLevel是最深层 
	for (int i = 1; i <= maxLevel; ++i)
		cout << " " << levelLeaves[i]; 

    return 0;
}

【PAT甲级】1004 Counting Leaves (30 分) JS 最近疫情在家办公,足不出户,做做PAT的题。奉上【PAT甲级】JS解题分析————第三篇。(1003Emergency暂时还不会,先跳过,哈哈) 阅读详情

相关推荐

Project 2: Counting Leaves

A family hierarchy等级 is usually presented by a pedigree tree系谱树. Your job is to count those family members who have no child.

【面试题】给定一个二叉树,求出它的最小深度,最小深度是从根节点到最近的叶子节点的最短路径的节点数。

解题:遍历一棵二叉树,从根部看起,查看它是否有左右结点。有五种情况 1.没有根节点,那结果就是0 2.有根节点,没有左右子树,结果为1 3.没有左子树,有右子树。把右子树看成一棵新的树,用子问题求解。 4.没有右子树,有左子树。把左子树看成一棵新的树,用子问题求解 5.既有左子树,又有右子树。那就把左右子树分别都看成新的树,最后比较谁的最近叶子的路径短,就取哪边。 因为都把左右...

mmwwxx123的博客 3767

Python海康相机api---超简单入坑学习必看

海康官网下载相机驱动​安装好以后找到Python的开发文件目录参考:D:\MVS\MVS\Development\Samples\Python\MvImport这几个文件很重要,想调用海康相机取图会用到.​为了写代码方便可以把这几个文件拷贝一下,跟你的api主文件放在同一个文件夹下可以减少80%的烦恼.​。

weixin_59360687的博客 3478

1004 Counting Leaves (30分)

1 题目 1004 Counting Leaves (30分) 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 o...

韩江雪de 小屋 638

GMSL2摄像头接入避坑指南:从FMC子卡配置到V4L2调用的完整流程(附Ubuntu20.04实测)

本文详细解析了GMSL2摄像头接入的全流程,从FMC子卡配置到V4L2调用的完整指南。基于XC7Z100平台实测经验,提供了硬件选型、带宽优化、Linux驱动配置及高分辨率采集的实战技巧,帮助开发者规避常见问题,提升系统稳定性与性能。

weixin_33737774的博客 418

[全志T113]:ST7701 86屏幕驱动

[全志T113]:ST7701 86屏幕驱动

LINKKEEE的博客 3141

PAT甲级刷题记录——1004 Counting Leaves (30分)

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

一只奋斗中的本科生 356

Ubuntu20.04下Docker无缝升级实战指南

本文提供了一份在Ubuntu20.04系统上安全、无缝升级Docker的实战指南。文章详细阐述了升级前的版本检查、数据备份等准备工作,逐步演示了通过APT包管理器进行升级或指定版本安装的操作步骤,并强调了升级后的验证与常见问题回滚方案,确保业务容器平稳过渡。

postgres8guard的博客 481

1004 Counting Leaves (30 分)

1004 Counting Leaves (30 分) 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 on...

qq_42711300的博客 531

从零开始:如何用Kimi OK Computer快速生成专业级数据分析报告和PPT(附实战案例)

本文详细介绍了如何利用Kimi OK Computer智能体模式,从零开始快速生成专业级数据分析报告和演示文稿。通过实战案例,解析了其任务规划、联网搜索、多工具协同和内容排版等核心能力,并提供了精准指令构造、工作流引导及成果精修的完整步骤,帮助用户高效完成从数据到成品的端到端交付。

weixin_29284201的博客 371

从 JDK8 到 JDK17:CRMEB 框架升级

【摘要】CRMEB开源框架已成功从JDK8升级至JDK17,带来显著性能提升:并发吞吐量提高35%,响应延迟降低40%,资源占用减少25%。升级方案解决87个技术卡点,支持模块化扩展和微服务转型,兼容SpringBoot3.0等新技术。提供基础版到企业级的升级服务,包含代码重构指导和性能调优。经金融级验证的方案可帮助电商平台实现日均10万+UV承载,支付成功率提升至99.7%。现以低于市场30%的价格提供全周期技术支持,助力企业抢占技术先机。

weixin_39782540的博客 701

PAT练习题(甲级) 1004 Counting Leaves (30分)(Java实现)

PAT练习题 1004 Counting Leaves(Java)题目题意解题思路代码实现 题目 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 inpu...

CoderHqf的博客 1万+

面向企业级应用的 SAP UI5 troubleshooting 全景指南:从日志与调试到性能与可测试性

本指南系统化介绍了SAP UI5应用排错的完整流程和工具链。从基础的浏览器开发者工具使用,到SAP UI5专用调试工具(Technical Information Dialog、Support Assistant、Diagnostics Window),再到性能优化和日志管理,提供了一套工程化的解决方案。重点推荐组合使用UI5 Inspector扩展、Test Recorder测试工具以及性能测量模块,并强调将常见问题整理成团队知识库。通过制度化的排错步骤和工具组合,开发者可以快速定位、复现和修复问题,将调

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 283

[PAT 甲级]1004 Counting Leaves (30 分) 【dfs 树的遍历】

1004 Counting Leaves (30 分) 原题链接 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 ...

cp0328的博客 3367

伺服系统控制原理深度解析

摘要:本文系统阐述了伺服系统的控制原理与应用。首先介绍了伺服系统的定义、组成及其在工业自动化中的重要性;然后详细分析了反馈控制机制、稳定性判据和性能指标;重点推导了传递函数和状态空间两种数学模型,并通过LaTeX公式展示了二阶系统建模过程;进而对比了PID控制、状态反馈及自适应控制等方法的优缺点;最后结合数控机床等案例,验证了理论方法的有效性,并展望了伺服系统智能化发展趋势。全文兼顾理论深度与实践指导性,为控制工程领域提供了系统的技术参考。

技术工作者 1266

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

浙大PAT 1004 Counting Leaves (简单深搜)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a

凌风 7021

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

PAT 1004 Counting Leaves

题目描述 英文 中文大意 通过输入的树找出 每一层有多少个叶子节点 样例输出 思路分析 用 dfs 深度优先遍历, 大致思路是寻找叶子节点, 如果找到在 record[] 数组对应的深度depth 加一 理解一下各个输入 N: 节点个数 M: 非叶子节点个数 ID: 非叶子节点标识 K: 此节点的儿子节点数 ID[i]: 此节点的儿子节点 首先接受 N、M, 叶子节点不用统计,所以根据M 获取节点 把每个节点的子节点都存入 vector[index] 通过判断vector[index].size()

唐五二七博客 187

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[C++] PAT 1004 Counting Leaves (30分)

Sample Input: 2 1 01 1 02 Sample Output: 0 1 题解: 给出一棵树,问每一层有多少个叶子节点,采用了DFS遍历 在#include <iostream> #include <vector> using namespace std; const int MAXN = 110; vector<int> T[MAXN];//T树结构 int n,depth[MAXN]={0};//n 节点数, depth[]深度 int .

快乐de约定的博客 2042

PAT-A1004 Counting Leaves 【DFS】

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

gdc6916的博客 3万+

PAT甲级 - 1004 Counting Leaves (30 分)

题目链接:(PAT甲级)1004 Counting Leaves (30 分) 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...

272

「PAT甲级真题解析」Advanced Level 1004 Counting Leaves

1. 题设要求按照从根结点开始自顶向下输出树结构每一层的叶子结点数目。 2. 这意味着我们需要设置计数器, 然后遍历树的每一个结点, 然后检查该结点是否是叶子结点, 如果是叶子结点, 则将计数器中该层次叶子结点个数+1. 3. 遍历树结构叶子结点可以用深度优先dfs搜索和广度优先搜索bfs.

过美好生活,做高效程序员 4994

PAT甲级1004 Counting Leaves (30分)

PAT甲级1004 Counting Leaves (30分) 题目: 思路:dfs,见注释 AC代码附上: #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int maxn=110; v...

weixin_43795683的博客 159

PAT 甲级 1004 Counting Leaves (DFS)(c++版)(附代码注释)

1004 Counting Leaves (30 分) 原文链接:http://kakazai.cn/index.php/Kaka/Pat/query/id/94 文章目录题目题意分析知识点与坑点一、DFS算法思路代码-c++版代码-python版 题目 题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/99480552...

kakazai.cn 377

PAT 甲级 1004(Counting Leaves)

PAT 甲级 1004题目要求翻译代码思路 题目要求 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 te...

qq_42865772的博客 388

PAT A1004 Counting Leaves

本程序为PAT A1004 Counting Leaves答案,题目链接。 主体思想:算法主要采用DFS算法,深度优先访问每一个结点,检查其是否为叶子结点。 详细介绍:   使用向量容器vector存储每个结点的所有孩子结点:v[ID].push_back(temp);   使用数组num[]记录每层的叶子结点数量;   使用深度优先搜索算法DFS,采用递归的方式,每次向深处前进时dep...

weixin_30340745的博客 115
上一篇: PAT1003 Emergency (25分) Dijstra
下一篇: PAT 1005 Spell It Right (20分) 字符数组做映射
wwang_dev
博客等级 码龄7年 27粉丝 76原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值