PAT甲级1004,1020解题报告

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

 

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

题目大意:给一棵树,求出各个层次中叶子节点的个数。

解题思路:趁这两天复习数据结构,树图的题目这两天写写完,其实树图就是套个模板。。尤其是树,几乎可以说凡树必递归。这题就DFS搜索一下这棵树就行了.

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
using namespace std;
map<int, vector<int>> tree;
map<int, int> res;
void Dfs(int node,int rank){
	//is leave
	if (tree[node].empty()) {
		res[rank]++;
		return;
	}
	for (auto i = tree[node].begin(); i != tree[node].end(); i++) {
		Dfs(*i, rank + 1);
	}
}
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		int pid, k;
		scanf("%d %d", &pid, &k);
		for (int j = 0; j < k; j++) {
			int id;
			scanf("%d", &id);
			tree[pid].push_back(id);
		}
	}
	Dfs(1, 0);
	int sum = 0;
	int num;
	for (int i = 0;; i++) {
		sum += res[i];
		if (sum == n - m) {
			num = i;
			break;
		}
	}
	for (int i = 0; i<=num; i++) {
		if (i != num)
			cout << res[i] << " ";
		else
			cout << res[i] << endl;
	}

	return 0;
}

1020 Tree Traversals (25 point(s))

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

题目大意:给个后序遍历和中序遍历,求出层次遍历。

解题思路:和做数据结构题时候差不多思路吧,先找根,然后左右分开,再继续找根···一个递归的过程就出来了,然后层次遍历就是个模板BFS一下。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
#include<queue>
using namespace std;
vector<int> post;
vector<int> in;
vector<int> lev;
struct node {
	int data;
	node* left;
	node* right;
};
node* buildTree(int postLeft, int postRight, int inLeft, int inRight) {
	if (postLeft > postRight) return NULL;
	int pos;
	for (int i = inLeft; i <= inRight; i++) {
		if (in[i] == post[postRight]) {
			pos = i;
			break;
		}
	}
	node* cur=new node;
	cur->data = post[postRight];
	cur->left = buildTree(postLeft, postLeft + pos - inLeft - 1, inLeft, pos - 1);
	cur->right = buildTree(postLeft + pos - inLeft, postRight - 1, pos + 1, inRight);
	return cur;
}
void Bfs(node* tree) {
	queue<node*> q;
	q.push(tree);
	while (!q.empty()) {
		node* cur = q.front();
		lev.push_back(cur->data);
		q.pop();
		if (cur->left !=NULL) q.push(cur->left);
		if (cur->right != NULL) q.push(cur->right);
	}
	return;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int cur;
		scanf("%d", &cur);
		post.push_back(cur);
	}
	for (int i = 0; i < n; i++) {
		int cur;
		scanf("%d", &cur);
		in.push_back(cur);
	}
	node* tree = buildTree(0, n - 1, 0, n - 1);
	Bfs(tree);
	for (int i = 0; i < n; i++) {
		if (i != n - 1) {
			printf("%d ", lev[i]);
		}
		else {
			printf("%d\n", lev[i]);
		}
	}
	return 0;
}

 

从柳婼与知乎经验谈PAT高效刷题:分类攻坚与逆向突破 本文结合柳婼的博客和知乎经验,探讨了PAT高效刷题的策略,包括分类攻坚与逆向突破。通过分析高频考点如动态规划、树结构和图算法,提供了具体的刷题路径和技巧,帮助考生优化备考效率。文章还分享了错题管理和资源利用的实用方法,助力考生在PAT考试中取得高分。 阅读详情

相关推荐

探究在linux下和windows下atoi函数在溢出处理的区别

最近在复习数据结构刷leetcode的时候突然出现了一个问题。 上面这是题目,当然简单的做肯定简单,我想用atoi和to_string函数,然后就出现问题了。 本来我在windos本机测试,当数字正溢出的时候,atoi返回的数字都是INT_MAX,然后就想当然拿aoti函数测试,如果返回的数字是INT_MAX的时候,就表示溢出了直接返回0. 然后提交之后,死活就不对,查看了一下执行结果,...

TateBrwonJava的博客 1087

1004 Counting Leave(甲级)

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&

Eighteen 217

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.

7-4 Counting Leaves(

7-4树叶 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. 家庭等级通常由谱系树表示。您的工作是计算没有孩子的家庭成员。 Input Specification: The input consists of...

qq_43791139的博客 313

别再瞎选了!手把手教你根据车型等级,在CarSim里快速匹配最合适的动力学模型

本文详细介绍了如何在CarSim中根据车型等级快速匹配最合适的动力学模型,提升仿真效率。通过分析CarSim模型库与A/B/C/D级车标准的对应关系,提供四步精准定位方法和不同车型的特殊处理技巧,帮助工程师避免常见错误,显著缩短模型准备时间。

weixin_27015733的博客 262

Adobe Acrobat Pro(Adobe PDF) - 使用技巧

Adobe Acrobat Pro为Adobe出品的PDF查看和编辑软件,下文简称Adobe PDF,该软件功能强大,拥有查看PDF、编辑PDF、打印PDF、导出PDF、合并文件、组织页面、文本标注、注释、动作向导、等功能。本文介绍Adobe PDF的一些使用技巧,最后介绍软件设置。

NSJim的博客 4万+

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

WorkBuddy 集成 飞书CLI,全程10分钟,丝滑无比,老板要注意了...

飞书CLI实测:10分钟实现AI操控飞书办公 飞书CLI作为命令行工具,让用户通过简单命令完成飞书各项操作。本文记录了从安装到使用的完整过程:一键安装仅需10分钟,支持200多条命令覆盖飞书全部功能,与AI结合后可实现自然语言办公。实测中成功用语音指令创建了7天发文计划日程,验证了其便捷性。虽然首次认证存在超时问题,但整体体验流畅。该工具将复杂操作简化为自然语言指令,大幅提升办公效率,特别适合需要频繁使用飞书的用户。后续还将探索更多自动化办公场景。

三棱球的专栏 1142

PAT真题练习(甲级)——1004Counting Leaves

PAT真题练习(甲级)——1004Counting Leaves

m0_61635175的博客 696

高速PCB设计中的一些常用规则

目前,高速PCB产品设计中,高速接口的应用日益普及,对于复杂的设计而言,高速走线就如同“蜘蛛网”一样密布在PCB各处,因此,在进行PCB检查及评审时,必要的高速PCB设计经验法则及规则是工程师开展工作必不可少的知识储备。

一只豌豆象的博客 3414

Qt应用软件【协议篇】自定义协议处理串口半包贴包

Qt串口数据,半包,贴包,丢包数据处理

小鱼酱的博客 1211

PAT甲级真题(并查集)——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...

WangXin Programmer 526

16、保障Azure应用安全:密钥管理与身份认证

本文深入探讨了保障Azure应用安全的关键措施,包括使用网络安全工具(如Application Gateway和Azure Front Door)防范攻击,解决传统密钥和机密存储方式的安全隐患,以及通过Azure Key Vault实现敏感信息的安全管理。同时,介绍了使用托管标识实现Azure资源间安全访问的方法,并结合实际操作步骤展示了如何配置Key Vault、设置访问策略、存储机密以及授予Web应用对SQL Server的访问权限。最后,对不同安全措施进行了比较,并提出了在开发、测试及生产环境中的综合

rock5的博客 71

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

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

NHS6671的博客 427

44位abogus补环境与纯算逆向分析

44位ab参数补环境与纯算加密流程逆向分析

吴秋霖的博客 2767

大模型部署指南:LM Studio

本文介绍使用LM Studio部署大模型

举世誉之而不加劝,举世非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。 1951

PTA A1003&A1004

第二天 A1003 Emergency (25 分) 题目内容 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount o...

Jason19750703的博客 302

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

1004. Counting Leaves

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

波大菜菜 377

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

1005 The Largest Generation (25)

题目描述 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.输入

Do not mess it up 379

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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 test case. Each case starts w...

宾斯基的博客 1051

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

valcanoshan的博客 389

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

iDestro的博客 266

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. 输入格式: Each input file contains one test case. Each...

乐行僧的博客 321

爆刷PAT(甲级)——之【1004】 Counting Leaves (30)——树上简单dfs搜索

艰难的英语单词: pedigree     英 [ˈpedɪgri:]      血统; 家谱 sake         英 [seɪk]    缘故, 目的 hence         英 [hens]    因此,所以 题意: 给一棵树,然后输出这颗树每一层的叶子节点数。 难点:N小于100。而且节点标记从1开始,根节点就是1,所以说难点就是如何统计出每一层的叶子数即可。 简单的想了一...

Hide_in_Code 556

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 Each input file contains one test case. Each case starts with

既然选择了远方 便只顾风雨兼程 578

PTA 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. 族谱通常由系谱树来表示。你的工作是统计那些没有孩子的家庭成员总数。 输入格式: Each input file contains one test case. Each case starts with a line containing 0<N<1

HaddockCaptain的博客 495
上一篇: 使用lftp进行文件备份
下一篇: 分布式配置中心选择
Tate_Brown
博客等级 码龄9年 0粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值