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 ...
随机推荐
- 安装Tidb数据库出现SSD硬盘IOPS不到40000的错误
今天安装tidb数据库出现IOPS过低的问题,这里如果仅仅是测试的话我们可以降低这个值,大概遇到的问题是: 解决方法: 1.我们在中控机的目录下修改某个配置文件: [tidb@:vg_adn_tidb ...
- 2019 wannafly winter camp
2019 wannafly winter camp Name Rank Solved A B C D E F G H I J K day1 9 5/11 O O O O O day2 5 3/11 O ...
- 13.4SolrCloud集群使用手册之CRUD
转载请出自出处:http://www.cnblogs.com/hd3013779515/ Student.java package cn.ljh.ssm.test; import org.apache ...
- Project configuration is not up-to-date with pom.xml
导入maven工程后,出现如下错误: Description Resource Path Location TypeProject configuration is not u ...
- 关于Spring IOC (DI-依赖注入)需要知道的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 <Spring入门经典>这本书无论对于初学者或者有 ...
- 最邻近规则分类(K-Nearest Neighbor)KNN算法
自写代码: # Author Chenglong Qian from numpy import * #科学计算模块 import operator #运算符模块 def createDaraSet( ...
- JS模拟下拉框select
最近做的一个项目有下拉框 同事都是用的是美化控件,但是用美化控件当然是好 但是网上找的一个控件不知道扩展性怎么样?对以后的维护会不会造成有影响?比如我想增加一个功能或者减少一个功能会不会影响?还有就是 ...
- MySQL(六)创建用户与授权
转载自:MySQL创建用户与授权 目录 一.创建用户 二.授权 三.设置和更改用户密码 四.撤销用户权限 五.删除用户 一.创建用户 命令: CREATE USER 'username'@'host' ...
- Android 一s个相对完整的自动升级功能实现代码
由于项目的需要最近做了一个关于Android自动升级的功能,下面将贴出Android手机客户端的完整代码.这段代码参考别的代码居多,由于不满足需求,所以自己仅仅改了一些需要变动的内容,其他功能都是按照 ...
- D. Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 ...