1004 Counting Leaves (30分)(一个字短!)

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 ... 阅读详情

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

题意

用邻接表输入一颗树,输出每层的叶子节点的数目

分析

层序遍历或者深度优先遍历

代码

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int n, m, id, k, a;
vector<int> tree[110], v;
int main() {
 	scanf("%d %d", &n, &m);
 	for (int i = 0; i < m; ++i) {
  		scanf("%d %d", &id, &k);
  		for (int j = 0; j < k; ++j) {
   			scanf("%d", &a);
   			tree[id].push_back(a);
  		}
 	}
 	int cnt;
 	queue<int> q;
 	q.push(1);
 	while (!q.empty()) {
  		queue<int> p;
  		cnt = 0;
  		while (!q.empty()) {
   			int f = q.front();
   			q.pop();
   			if (tree[f].size() == 0)++cnt;
   			for (int i = 0; i < tree[f].size(); ++i)p.push(tree[f][i]);
  		}
  		q = p;
  		v.push_back(cnt);
 	}
 	for (int i = 0; i < v.size(); ++i)
  		printf("%d%s", v[i], i == v.size() - 1 ? "" : " ");
 	return 0;
}
Win10 + Linux 双系统删除(Linux) 删除Linux所使用的磁盘分区 进入此电脑->计算机->管理->磁盘管理 阅读详情

相关推荐

【SABRE 3D, SABRE 3DxT,LAM设备】2025年9月10日

前文:SABRE 3D系列机台简介本章节详细阐述了在维护期间锁定电气、机械、热力学及重力组件以及化学管线输入的锁定操作流程,旨在降低人员受伤风险。p42~p55。

weixin_46735260的博客 1616

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

SourDumplings的博客 3921

Ubuntu 24.04 下 NoMachine 极速安装与远程桌面配置全攻略

本文详细介绍了在Ubuntu 24.04系统下安装和配置NoMachine远程桌面的完整指南。从系统准备、分步安装到性能调优和常见问题解决,帮助用户快速实现低延迟的远程桌面访问。特别适合开发者和IT管理员在Linux环境下高效部署远程工作环境。

moon9的博客 945

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万+

告别ROS1!在Ubuntu 22.04上为VINS-Fusion适配ROS2 Humble的完整踩坑记录

本文详细记录了在Ubuntu 22.04上将VINS-Fusion从ROS1迁移到ROS2 Humble的完整过程,包括环境搭建、依赖管理、核心代码适配及性能优化。通过实战案例和调试技巧,帮助开发者高效解决迁移过程中的常见问题,提升系统稳定性和性能。

weixin_42628688的博客 281

工业数据采集实战:用open62541库读写PLC的OPC UA节点(避坑指南)

本文详细介绍了如何使用open62541库实现工业PLC的OPC UA节点数据采集,涵盖环境搭建、节点读写优化及生产环境故障排查。通过订阅模式和多节点读写技术,显著提升数据采集效率与稳定性,适用于智能制造和工业物联网场景。

weixin_30598225的博客 400

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 one t...

92377 421

单片机如何获得5V电源

在单片机实际设计中,有时会需要在电路中用到5V电压源,那么这个常见的实验需求有哪些办法解决呢,我综合网上的资料,概括一下: 1)USB    这种方法在我们直接将单片机板与电脑连接调试的时候,算是简单又方便的办法了,但如果我所要得到的成品不直接连接外部的器件,这就得舍弃。     常用的,USB/5V插头线,这是一种专门的电源线,一端是USB通用接口,可以直插USB端口,另一端是内正外负电压

Echo_Ana的博客 3万+

PAT甲级 1004 Counting Leaves

PAT甲级 1004 Counting Leaves 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...

旺崽的博客 2850

华为OD机考双机位C卷 - 朋友圈个数(Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 朋友圈个数(OD上机考试双机位C卷)。提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。

算法大师! 695

【华为OD技术面-手撕算法代码真题】- 最长公共前缀

🌟 强烈推荐:华为OD技术面试手撕算法代码真题 🌟大家好!今天我给大家推荐一份备受赞誉的华为OD技术面试手撕算法代码真题。所有题目均为华为od实际面试过程中出现的算法代码真题。

算法大师! 4885

PAT 甲级 1004  Counting Leaves

1004 Counting Leaves (30 point(s)) 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 cont...

漫浸天空的雨色的备忘录~ 1491

[AI8051U入门第一步]环境安装和按键控制流水灯

本文介绍了AI8051U单片机的开发环境搭建及GPIO应用。主要内容包括:1)通过ISP软件完成Keil开发环境配置;2)AI8051U特性介绍,包括32位8051内核、浮点运算单元及丰富外设;3)GPIO输入输出模式解析,并附流水灯控制代码;4)提供完整的系统初始化、延时函数及LED控制代码实现。文章配套资源包含完整工程代码,适用于智能硬件及物联网开发。

sinat_58149788的博客 672

PAT(甲级)1004 Counting Leaves (30分)

题目来源:1004 Counting Leaves (30分) 题目描述: Sample Input: 2 1 01 1 02 Sample Output: 0 1 题目翻译: 一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。 输入 每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指树中的结点个数(0<N<100),M指非叶结点的个...

sinat_41835935的博客 274

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

【PAT甲级】1004 Counting Leaves (30 分)

1004 Counting Leaves 题目描述 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 tes...

初见还是重逢的博客 312

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

1004 Counting Leaves (30 分)

1004 Counting Leaves (30 分) 题目大意: 判断一个树 每一层 叶子节点的个数 分析: 树上的DFS 或者 BFS,这里采用DFS,每深入一层,总深度加1 如果下一层有孩子,继续向下,如果没有就ans[层数]++; 其中用到了vector,不懂得建议看一看STL AC代码: #include &lt;iostream&gt; #include &lt;vector&gt; ...

Fighting_Eagle的博客 253

1004 Counting Leaves C语言

@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 contai...

fhjilulu 292

1004 Counting Leaves (30)(30 分)

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

aboc43983的博客 161

Day2: [PAT] 1004 Counting Leaves (30 分)

Day2: [PAT] 1004 Counting Leaves (30 分) 题源: 来自PAT甲级题库1004: https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 思路: 该题考查通过队列对一颗树进行层序遍历,要注意的有两点: 1在读入树的时候就可以记录各个节点的出度入度情况,以便查找...

qq2215459786的博客 142

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

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

A1004 Counting Leaves (30 分)

1004 Counting Leaves (30 分) 解题思路        题目以家谱树为背景,真实的目的是以层为单位,计算叶子结点的数目。虽然考察的是树的相关内容,但是不需要建树。只需要借用层次遍历的思路就可以确定每个结点所属的层。然后使用哈希表在输入的时候标记非叶结点,剩下的就是叶子结点。 AC代码 #include <cstdio> #...

LinxRds的博客 299

PAT 1004 Counting Leaves

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

唐五二七博客 187
上一篇: 1003 Emergency (25分)(一个字短!)
下一篇: 1005 Spell It Right (20分)(一个字短!)
大了七八个
博客等级 码龄8年 2粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值