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 ...
随机推荐
- jquery实现根据所选时间生成页面元素
最近做项目,碰见这样的一个需求 根据所选的时间动态生成值班安排,日期格式需要带星期,如: 代码如下: 1.首先放两个文本框,时间插件用的是My97DataPicker,再放一个table,简单设置一下 ...
- beta冲刺————第五天(5/5=1)
今天的主要内容是前后端的对接: 通过前几天的对接,我们发现后端传给前端内容是可以很完美的显示出来的,说明文章格式以及一些默认规则都是OK的. 然后就是前端从云服务器上面接受到文章的具体内容,在这一个环 ...
- 【Ansible 文档】配置
http://docs.ansible.com/ansible/latest/intro_configuration.html http://docs.ansible.com/ansible/late ...
- AIX平台安装Oracle11gR2数据库
1. 前提条件 1.1 认证操作系统 Certification Information for Oracle Database on IBM AIX on Power systems(Doc ID ...
- [SDOI2008]洞穴勘测
嘟嘟嘟 写完lct的板儿后觉得这就是一道大水题. 连pushup都不用. 不过还是因为一个zz的错误debug了一小会儿(Link的时候连出自环--) 还有一件事就是Cut的时候判断条件还得加上,因为 ...
- cenos6.5 python2.6.6升级至python2.7.3
踩坑无数... 一.参照以下两篇博文把python升级至python2.7.3 https://www.cnblogs.com/senzhe/p/6322214.html http://www.jb5 ...
- 【转】浅谈一个网页打开的全过程(涉及DNS、CDN、Nginx负载均衡等)
1.概要 从用户在浏览器输入域名开始,到web页面加载完毕,这是一个说复杂不复杂,说简单不简单的过程,下文暂且把这个过程称作网页加载过程.下面我将依靠自己的经验,总结一下整个过程.如有错漏,欢迎指正. ...
- JSP中使用JDBC连接MySQL数据库的详细步骤
1,首先在自己新建的项目文本框中输入Web Project的名称,然后单击下一步. 2,继续单击下一步 3,把Generate web.xml deployment descriptor复选框勾上. ...
- CentOS7下安装NVIDIA独立显卡驱动出现X service error问题解决方法
问题症状: 最近在CentOS7下安装NVIDIA独立显卡驱动的过程中出现X service error问题,如下图所示: 解决方法: 0.到NVIDIA 官网上下载驱动文件(.run 格式) : N ...
- WorldWind源码剖析系列:星球表面渲染类WorldSurfaceRenderer
星球表面渲染类WorldSurfaceRenderer描述如何渲染星球类(如地球)表面影像纹理.该类的类图如下. 星球类World包含的主要的字段.属性和方法如下: public const int ...