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
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
题目描述:
统计一颗树每一层的leaf数量。
算法分析:
思路1:BFS
本质就是lever order traversal, 可以用bfs遍历,然后每一层统计叶子数。
思路2:DFS
可以使用邻接矩阵的方式定义树结构。然后使用 dfs 遍历树的节点,并记录每层的叶子节点数量。 可以看到,时间空间的 trade-off 不仅仅是性能上的提升,也会影响带代码实现的复杂程度。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; #define MX 101 int mp[MX][MX];
queue<int> que;
int n,m; int bfs(int s) {
int flag = ;
for (int i=; i<=n; i++) {
if (mp[s][i] == ) {
que.push(i);
flag = ;
}
}
return flag;
} void actbfs() {
que.push();
que.push();
int cnt = ;
while (!que.empty()) {
int s = que.front();
que.pop();
if (s == ) {
if (que.empty()) {
printf("%d", cnt);
break;
}
else {
que.push();
printf("%d ", cnt);
cnt = ;
}
}
else {
int flag = bfs(s);
cnt += flag;
}
}
} int main()
{
scanf("%d%d", &n,&m);
memset(mp, , sizeof(mp));
for (int i=; i<m; i++) {
int id,k;
scanf("%d%d", &id,&k);
for (int j=; j<k; j++) {
int chi;
scanf("%d", &chi);
mp[id][chi] = ;
}
} actbfs(); return ;
}
PAT 解题报告 1004. Counting Leaves (30)的更多相关文章
- PAT 解题报告 1049. Counting Ones (30)
1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...
- PAT (Advanced Level) 1004. Counting Leaves (30)
简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
随机推荐
- 解析使用ThinkPHP应该掌握的调试手段
解析使用ThinkPHP应该掌握的调试手段 使用ThinkPHP应该掌握的调试手段经常看到有人问到findAll的返回数据类型是什么之类的问题,以及出错了不知道什么原因的情况,其实还是没有熟悉 ...
- CentOS上安装Node.js
CentOS上安装Node.js [日期:2014-07-21] 来源:Linux社区 作者:maskdfe [字体:大 中 小] CentOS上安装Node.js(想在Linux上学习No ...
- 20145235 《Java程序设计》第10周学习总结
教材学习内容总结 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 程序员所作的事情就是把数据发 ...
- 【转】Unity 之 移动设备的触控操作
http://blog.csdn.net/anyuanlzh/article/details/18367941 这篇博文将简单的记录,如何用unity处理在移动设备上的触控操作. iOS和And ...
- Bootstrap 模态框插件
一.基本使用 使用模态框的弹窗组件需要三层 div 容器元素,分别为 modal(模态声明层). dialog(窗口声明层).content(内容层).在内容层里面,还有三层,分别为 header(头 ...
- Bootstrap 表单和图片 (内联表单,表单合组,水平排列,复选框和单选框,下拉列表,校验状态,添加额外的图标,控制尺寸,图片)
一.表单 基本格式 注:只有正确设置了输入框的 type 类型,才能被赋予正确的样式. 支持的输入框控件 包括:text.password.datetime.datetime-local.date.m ...
- Redis学习笔记(1)-Key
package cn.com; import java.text.ParseException; import java.util.List; import java.util.Set; import ...
- EF6 CodeFirst 启用Migration,常用命令
Enable-Migrations –EnableAutomaticMigrationsAdd-Migration [MigrationName] [-Force]Update-Database –T ...
- 【tips】判断两个整数是否是同一个数量级
leetcode刷题的时候,需要用到,已知整数A,B,且A>B,判断AB是否是同一数量级的. 第一想到的是不停地除以10,得到每个数字的数量级再进行比较,太麻烦: 转而向转化成字符串比较,还是麻 ...
- (leetcode)Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...