PAT A1004 Counting Leaves (30 分)——树,DFS,BFS
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, the number of nodes in a tree, and M (<), 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 01level, 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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int tree[][] = { -1 };//只会赋给第一个元素,别的默认为0
//fill(tree[0], tree[0] + 101*101, -1);
int leaf[] = { };
int step = ;
void dfs(int i, int depth){
if (tree[i][] == -1){
leaf[depth]++;
return;
}
int j = ;
while (tree[i][j] != -1){
dfs(tree[i][j], depth + );
j++;
}
step = max(step, depth + );
}
int main(){
int n, m;
fill(tree[0], tree[0] + 101*101, -1);
cin >> n >> m;
for (int i = ; i < m; i++){
int root, num;
cin >> root >> num;
for (int j = ; j < num; j++){
int leaf;
cin >> leaf;
tree[root][j] = leaf;
}
}
int root = , depth = ;
dfs(root, depth);
for (int i = ; i <= step; i++){
if(i!=step)cout << leaf[i] << " ";
else cout << leaf[i];
}
system("pause");
}
注意点:一开始题目都没看懂,给的例子太不具代表性了,读了好多遍终于知道是要求每层的叶节点个数并输出。就是考了一个深度优先搜索(DFS),居然还不会做,DFS要用递归一层层遍历,遍历完要记录最深到几层了,方便后面输出。这里建树用了二维数组,其实有点蠢,用vector数组会方便一点。另外,二维数组的初始化只能用fill或memset,直接像一维数组那样给一个值只会赋给数组第一个元素。
PAT A1004 Counting Leaves (30 分)——树,DFS,BFS的更多相关文章
- 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分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- 1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- 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 ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
随机推荐
- Velocity快速入门
Velocity 介绍 Velocity是一个基于java的模板引擎.它允许任何人使用简单但功能强大的模板语言引用Java代码中定义的对象. 当Velocity用于web开发时,web设计人员可以与J ...
- JS写的二级导航栏(利用冒泡原理)
今天给大家分享一种用JS写的导航栏,虽然我们工作中不会使用JS来做导航栏,为了练习我们用JS来做一个JS导航栏 这种方法要比其他方法代码量少很多,但是需要对事件冒泡有一定的理解,如果需要理解冒泡可以参 ...
- CentOS7下安装caffe(包括ffmpeg\boost\opencv)
因为有项目想采用深度学习,而caffe是深度学习框架中比较理想的一款,并且跨平台,以及可以采用python/matlab的方式进行调用等优势,所以想在服务器上安装,下面就开始了血泪史... 服务器是阿 ...
- 解读 --- 基于微软企业商务应用平台 (Microsoft Dynamics 365) 之上的人工智能 (AI) 解决方案
9月25日微软今年一年一度的Ignite 2017在佛罗里达州奥兰多市还是如期开幕了.为啥这么说?因为9月初五级飓风厄玛(Hurricane Irma) 在佛罗里达州登陆,在当地造成了挺大的麻烦.在这 ...
- Android4.4中jni的native的方法无法找到的解决方案
1.禁用代码混淆功能 LOCAL_PROGUARD_ENABLED:= disabled 2.修改混淆规则,对于类的native 方法 不要进行混淆
- Android 2018最新的三方库
文章出处https://blog.csdn.net/qq_32368129/article/details/78749880 1.MaterialStepperView 它是用Material Des ...
- servlet和jsp页面过滤器Filter的作用及配置
刚刚有个朋友问我,Servlet的过滤器有什么作用? 现在发个帖子说明一下, 过滤器是一个对象,可以传输请求或修改响应.它可以在请求到达Servlet/JSP之前对其进行预处理, ...
- fiddler常见的应用场景
在移动互联网时代,作为软件测试工程师,fiddler绝对是值得掌握并添加进技术栈里的工具之一. 那么,fiddler在日常的测试工作中,一般都有哪些常见的应用场景呢? 根据以往工作经验,大概有如下4类 ...
- python常用模块之-random模块
random模块顾名思义就是生成随机数的模块. random模块有以下常见方法: 1,打印0-1之间的任意随机浮点数,不能指定区间. print(random.random()) 2,打印随机符点数, ...
- 分享几款常用的MySQL管理工具
MySQL数据库以体积小.速度快.总体拥有成本低等优点,深受广大中小企业的喜爱,像我们常见的MySQL管理工具都有那些呢?下面给大家推荐六个常用的MySQL管理工具! phpMyAdmin ...