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基础之---mysqlimport工具和LOAD DATA命令导入文本文件
1.mysqlimport工具的使用 看一下命令的使用方法: shell > mysqlimport -u root -p [--LOCAL] DBname File [option] --f ...
- 在 CentOS/Fedora 下安装 JAVA 环境
介绍 本文介绍如何在 CentOS 7(6/6.5). Fedora.RHEL 上安装 Java.Java是一个流行的软件平台,允许您运行Java应用程序. 本文涵盖了以下Java版本的安装: Ope ...
- python提示警告InsecureRequestWarning
在Python3中使用以下代码报错: import requests response = requests.get(url='', verify=False) 错误代码如下: InsecureReq ...
- 2.2.3 TableLayout(表格布局)
3.如何确定行数与列数 ①如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!! ②如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面! ③ ...
- Python基础(11)——反射、异常处理
1.反射 以下均是对对象的操作,而不是对类 class Foo(object): def __init__(self): self.name = 'wupeiqi' def func(self): r ...
- 信号处理开源库SP++介绍
SP++ (Signal Processing in C++) 是一个关于信号处理与数值计算的开源 C++程序库,该库提供了信号处理与数值计算中常用算法的 C++实现.SP++中所有算法都以 C++类 ...
- Python2.7-math, cmath
math,cmath 模块,提供了用C标准定义的数学函数,简单说就是效率较高,cmath 不仅有 math 的功能,还增加了计算复数的函数.这两个模块返回的值基本上为 float 类型,除非明确指出返 ...
- Android放大镜效果的简单实现
package com.example.myapi.pictobig; import com.example.myapi.R; import android.content.Context; impo ...
- 【转】常见的Web实时消息交互方式和SignalR
https://www.cnblogs.com/Wddpct/p/5650015.html 前言 1. Web消息交互技术1.1 常见技术1.2 WebSocket介绍1.3 WebSocket示例 ...
- 蓝桥杯 历届试题 约数倍数选卡片 (经典数论+DFS)
闲暇时,福尔摩斯和华生玩一个游戏: 在N张卡片上写有N个整数.两人轮流拿走一张卡片.要求下一个人拿的数字一定是前一个人拿的数字的约数或倍数.例如,某次福尔摩斯拿走的卡片上写着数字“6”,则接下来华生可 ...