PAT-1004 Counting Leaves
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<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
算法说明:
算法要求求出每一层没有孩子节点的个数并依次输出,本文采用vector存储结点,当然你可以自己定义结构体。然后用递归计算每一层的没有孩子结点的个数,这时就必须有个数组book[depth]来记录啦,下标代表层数,对应的值表示这一层无孩子结点个数。递归停止的条件为 :vector[index].size()==0 说明当前结点没有孩子结点,使book[depth]++,下面贴出代码。

**树结构与vector存储**
```c++
// 1004 Counting Leaves.cpp : Defines the entry point for the console application.
//
include "stdafx.h"
include
include
using namespace std;
vector vec[100];
int maxDepth=-1;
int book[100]={0};
/**
4 2
01 2 02 03
02 1 04
*/
void dfs(int index,int depth){
if(vec[index].size()==0){
book[depth]++;
if(depth>maxDepth)
maxDepth=depth;
return;
}
for(int i=0;i<vec[index].size();i++)
dfs(vec[index].at(i),depth+1);
}
int main(int argc, char argv[])
{
int N,M,node,k,leaf;
cin >>N>>M;
for(int i=0;i<M;i++){
cin >>node>>k;
for(int j=0;j<k;j++){
cin >> leaf;
vec[node].push_back(leaf);
}
}
dfs(1,0);
cout<<book[0];
for(int j=1;j<=maxDepth;j++){
cout<<" "<<book[j];
}
return 0;
}
**<center>2020考研打卡第二天,你可知道星辰之变,骄阳岂是终点?千万不要小看一个人的决心。加油!!!</center>**
**<center>将来的你一定会感谢现在奋斗的自己</center>**
PAT-1004 Counting Leaves的更多相关文章
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- 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 ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
随机推荐
- 【MYSQL】语法复习
一.数据类型 截图来源: http://www.runoob.com/mysql/mysql-data-types.html 二.基本语句 1.创建数据表 -- 主键自增,T_User CREATE ...
- sysbench压力工具报错:
[root@ sysbench-]# /usr/local/sysbench/bin/sysbench --version : cannot open shared object file: No s ...
- ASCII码查看
字母对照表: ASCII可显示字符: ASCII控制字符:
- 【2017下集美大学软工1412班_助教博客】个人作业2——APP案例分析
作业要求 个人作业2:APP案例分析 评分结果 按从高到低排列 学号后三位 第二次作业 Total 008 APP案例分析 23 044 第2次作业 19.5 011 App案例分析--XBMC 19 ...
- Swift 并行编程现状和展望 - async/await 和参与者模式
这篇文章不是针对当前版本 Swift 3 的,而是对预计于 2018 年发布的 Swift 5 的一些特性的猜想.如果两年后我还记得这篇文章,可能会回来更新一波.在此之前,请当作一篇对现代语言并行编程 ...
- post-message-stream的学习-metamask
kumavis/post-message-stream post-message-stream Sets up a duplex object stream over window.postMessa ...
- MyBatis实战之动态SQL
如果使用JDBC或者其他框架,很多时候你得根据需要去拼接SQL,这是一个麻烦的事情,而MyBatis提供对SQL语句动态的组装能力,而且它只有几个基本的元素,非常简单明了,大量的判断都可以在MyBat ...
- WinForm中Component Class、User Control及Custom Control的区别和使用
NET Framework 为您提供了开发和实现新控件的能力.除了常见的用户控件外,现在您会发现,您可以编写能执行自身绘图的自定义控件,甚至还可以通过继承扩展现有控件的功 能.确定创建何种类型的控件可 ...
- WorldWind源码剖析系列:下载请求类DownloadRequest
下载请求类DownloadRequest是各种下载请求的抽象基类,先派生出网络下载请求类WebDownloadRequest,再派生出地理空间下载请求类GeoSpatialDownloadReques ...
- windows安装wget
windows安装wget1. 下载wget-1.11.4-1-setup.exehttps://jaist.dl.sourceforge.net/project/gnuwin32/wget/1.11 ...