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 ...
随机推荐
- 初学HTML-3
标题标签:<h#>...</h#>,从h1到h6,字号由大变小. 段落标签:<p>...</p>,在浏览器中独占一行. 空格:" " ...
- vue 结合 animate.css
这里说的是vue2.0和animate.css的结合使用.其实用过就知道用法是比较简单的.但是就是刚开始使用的时候,难免有的会遇到各种问题.简单的说说我所用过并且遇过的坑. 首先是transition ...
- 网络基础 HTTP协议之HTTP消息(HTTP Message)
HTTP协议之HTTP消息(HTTP Message) by:授客 QQ:1033553122 1. 消息类型(Message Type) HTTP messages包含从客户端到服务器的请求和服 ...
- MySql 定时任务的使用
MySql 定时任务的使用 by:授客 QQ:1033553122 简介 自 MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定 ...
- 双启动:安装Windows 7 和 CentOS 7 双系统教程
笔记本配置:8G内存,200G SSD,先在virbox中成功安装双系统,能正常进入并使用 Windows 7 和 CentOS 7. 网上看到一大堆的安装 wingrub easyBCD,折腾了一 ...
- 【已解决】mac上appium报错:“Could not find aapt Please set the ANDROID_HOME environment variable with the Android SDK root directory path”
按照网上教程配置完appium环境后,真机跑自动化过程,遇到如下报错: appium报错如下: [ADB] Checking whether aapt is present [ADB] The AND ...
- LeetCode题解之Copy List with Random Pointer
1.题目描述 2.问题分析 首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可. 3.代码 RandomListNode *copyR ...
- Sql server的Merge语句,源表中如果有重复数据会导致执行报错
用过sql server的Merge语句的开发人员都应该很清楚Merge用来做表数据的插入/更新是非常方便的,但是其中有一个问题值得关注,那就是Merge语句中的源表中不能出现重复的数据,我们举例来说 ...
- win Server 2008 笔记
1.开启tsmmc 远程登录连接 需要在入站规则中启用一下规则 远程管理(RPC-EPMAP) 远程管理(RPC) 远程管理(RPCNP-IN) 远程管理(TCP-IN) 远程管理 - RemoteF ...
- 为MySQ启用HugePage
8.12.4.2 Enabling Large Page Support Some hardware/operating system architectures support memory pag ...