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

Output

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
思路:把树当做图存储在邻接矩阵中;然后广度优先遍历邻接矩阵;
需要注意的是多push了一个0作为分隔层与层的标志
代码如下:
#include <iostream>
#include<queue>

using namespace std;
#define MAX 101
//根节点是从1开始的,所以数组也按找这样去存
int map[MAX][MAX] = {0};
int i;
int visit_isleaf(int aa)
{
	for (int k = 1;k < MAX;k++)
	{
		if (map[aa][k] == 1)
			return 0;
	}
	return 1;
}
void bfs()
{
	queue<int > q;
	q.push(1);
	q.push(0);
	int counter = 0;
	while (!q.empty())
	{
		int temp = q.front();
		q.pop();
		if (temp == 0)
		{
			if (q.empty())
			{
				cout << counter;
				break;
			}
			else
			{
				q.push(0);
				cout << counter<<" ";
				counter = 0;
			}
		}
		else
		{
			counter += visit_isleaf(temp);
			for (i = 1;i < MAX;i++)
			{
				if (map[temp][i] == 1)
				{

					q.push(i);
				}
			}
		}
	}
}

int main()
{
	int n, m;
	cin >> n >> m;
	for ( i = 0;i < m;i++)
	{
		int id, k;
		cin >> id >> k;
		for (int j = 0;j < k;j++)
		{
			int id2;
			cin >> id2;
			map[id][id2] = 1;
		}
	}

	bfs();
	
	return 0;
}


ml307 二次开发 中移物联网旗下4g模组官方网站官方教程id=514官方客户不知道啥时候上班 早上10点也说不在服务时间内 找不到官方的sdk下载路径。 阅读详情

相关推荐

OBS技术深度剖析:从开源直播工具到专业制作引擎的进化之路

本文深入剖析了开源直播软件OBS的技术架构与发展历程。从2012年诞生至今,OBS已成长为直播领域的事实标准,其成功源于模块化设计和开源协作模式。文章详细解析了OBS的核心技术:1) 分层架构与插件系统,2) GPU加速的渲染管线,3) 智能音频处理,4) 多平台编码优化。同时探讨了虚拟摄像头、浏览器源等高级功能的实现原理,并提供了性能优化方案和硬件配置建议。随着AI、WebRTC等新技术发展,OBS将持续引领实时内容创作工具的创新,为开发者和创作者提供更强大的技术支持。

程序猿老樊的博客 328

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

从零到一:在Ubuntu 20.04上搭建ROS1与PX4无人机开发环境

本文详细指导如何在Ubuntu 20.04系统上搭建完整的ROS1与PX4无人机开发环境。内容涵盖ROS Noetic安装、PX4固件源码编译、MAVROS通信桥接以及QGroundControl地面站的配置,并通过Gazebo仿真完成全链路联调测试,为无人机算法开发与验证提供稳定可靠的基础平台。

weixin_29292431的博客 167

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

OBD诊断协议:OBDⅡ (SAE J1979) 和 OBDonUDS (SAE J1979-2) 和ZEV OBD技术架构的详细对比

摘要:文章对比了2025年最新OBD技术标准(SAEJ1979系列)的三大架构:传统OBD-II(已淘汰)、OBDonUDS(燃油/混动车)和ZEVOBD(新能源车)。核心发现:1)OBDonUDS与ZEVOBD共享UDS协议栈和DoIP通信标准,仅应用层数据(DID/DTC)不同;2)物理层统一为CAN+以太网,支持远程诊断和OTA;3)诊断工具需同时兼容J1979-2/3数据库。技术演进呈现"协议统一化、数据专用化"趋势,2027年后所有新车将强制采用新标准。

m0_56482679的博客 2395

JDK8升级JDK17过程中遇到的那些坑

JDK8虽然非常好,但是JDK版本已经发布到JDK20了,且JDK8后的版本升级了很多新的特性,如模块化、ZGC以及虚拟线程、结构性并发等,也是非常有吸引力的,所以决定将基于JDK8的项目升级到最近的LTS版本JDK17。

BASK2312的博客 6685

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

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

D_D_L_L_的博客 294

强化学习代码实现PPO

梯度更新:通过截断(clipping)限制策略更新幅度,避免策略变化过大影响稳定性。首先是数据预处理,将相关数据(状态、采取的动作、状态奖励、下一个状态)放到gpu上。根据当前状态 state,利用策略网络生成每个动作的概率分布 probs。2. 修改了策略模型的输出,从每个动作的概率分布变成了动作的均值和方差。1. 修改了动作空间,离散空间变成了连续空间,使用正太分布来采样。3. 修改了动作的ratio的计算,从离散分布变成了正太分布计算。Update函数:通过经验缓冲数据来优化策略模型和价值模型。

wlxsp的博客 636

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

浅谈MOS管的发热原因和解决办法

解读一下开关管发热原因,解决方案和开关损耗测试。

qq_45508321的博客 1万+

链路聚合Eth-trunk以及华为交换机配置实例

链路聚合简介 以太网链路聚合Eth-Trunk简称链路聚合,它通过将多条以太网物理链路捆绑在一起成为一条逻辑链路,从而实现增加链路带宽的目的。同时,这些捆绑在一起的链路通过相互间的动态备份,可以有效地提高链路的可靠性。 目的: 随着网络规模不断扩大,用户对骨干链路的带宽和可靠性提出越来越高的要求。在传统技术中,常用更换高速率的接口板或更换支持高速率接口板的设备的方式来增加带宽,但这种方案需要付出高额的费用,而且不够灵活。 采用链路聚合技术可以在不进行硬件升级的条件下,通过将多个物理接口捆绑为一个逻辑接口

ChaseAug的博客 2万+

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

川崎机器人 AS语言基础运动指令表

    功能简介 动作方式 参考坐标系 位置特性 X,Y,Z 角度特性 O,A,T 指令格式(距离单位毫米,时间单位秒,角度单位度 示例1 示例2 运 动 指 令 JMOVE 以关节移动到目标点 关节移动方式 基础坐标系 不受控 伪随机动作 JMOVE 目标点 JMOVE #yd JMOVE put1 LMOVE 以直线移动到目标点 直线移动方式

喜欢自学并分享经验的技术宅 9353

A1004 Counting Leaves (30 分)

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

LinxRds的博客 299

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

1004.Counting Leaves (30)

1004.Counting Leaves (30)pat-al-10042017-02-03 深搜和广搜都可以 /** * pat-al-1004 * 2017-02-03 * Cpp version * dfs * Author: fengLian_s */ #include<stdio.h> #include<vector> using namespace std; vector<i

一剑钦安 460

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

PAT 1004. Counting Leaves (30)

http://www.patest.cn/contests/pat-a-practise/1004 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 ch...

dengkonglou4529的博客 127

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

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

【C++】1004. Counting Leaves (30)

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

artemisrj的专栏 715
上一篇: 图的各种操作总结
下一篇: char *a 与char a[] 的区别
iking83
博客等级 码龄12年 8粉丝 48原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值