PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves
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 1leaf node. Then we should output 0 1 in a line.
Sample Input:
2 1
01 1 02
Sample Output:
0 1
题目解析
本题给出一颗家族关系树第一行首先给出两个整数0<N<100为家族中人数,M<N为非叶子节点个数(有孩子的人的数量)之后跟随M行,每行都是一个有孩子的人的信息,其中包括一个两位id为其对应编号,一个整数K为其孩子的数量,之后K个两位数为其孩子的编号。家族树的根结点编号为1,要求由根结点所在层开始输出每层拥有的叶子结点个数(每层没有孩子的成员个数)。
用一个vector<int> 类型的数组child[ ]储存每个人的孩子的编号,int型数组cnt[ ]储存每层的叶子结点数量,视根结点为第0层由根结点开始dfs搜索其每一个孩子,每个点的孩子所在的层数就是其层数加一,若搜索到没有孩子的结点便将其对应层叶子结点数量加一。将搜索到的最大层数计入maxL。
最后由0到maxL输出每层的叶子结点数量即可。
#include <bits/stdc++.h>
using namespace std;
vector<int> child[]; //储存每个id的孩子
int cnt[]; //记录每层叶子结点的个数
int n, m; //n为总人数, m为非叶子结点数量
int maxL = INT_MIN; //maxL记录最大层数
void dfs(int id, int nowlevel){
maxL = max(maxL, nowlevel); //记录最大层数
if(child[id].empty()) //如果该点没有孩子表明其为叶子结点
cnt[nowlevel]++; //其对应层的叶子结点数量加一
for(auto i : child[id]) //搜索其所有的孩子
dfs(i, nowlevel + );
}
int main()
{
scanf("%d%d", &n, &m); //输入总人数与非叶子结点数
for(int i = ; i < m; i++){ //输入所有非叶子结点信息
int id, k; //该点id与孩子数量k
scanf("%d%d", &id, &k);
for(int j = ; j < k; j++){ //输入该点所有孩子
int cid;
scanf("%d", &cid);
child[id].push_back(cid); //记录该点的孩子
}
}
dfs(, ); //1为根结点,由根结点第0层开始搜索
for(int i = ; i <= maxL; i++){
if(i != )
printf(" ");
printf("%d", cnt[i]);
}
return ;
}
PTA (Advanced Level) 1004 Counting Leaves的更多相关文章
- PAT (Advanced Level) 1004. Counting Leaves (30)
简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- 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 Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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 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 ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
随机推荐
- Beta阶段第六篇Scrum冲刺博客-Day5
1.站立式会议 提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 张晨晨:完善收藏功能 郭琪容:收藏功能的实现 吴玲:完 ...
- noip第25课作业
1. 求一个有向图所有顶点入度的和 输入有向图的顶点个数,边数以及各顶点之间的关联情况,要求求出这个有向图的所有顶点入度的总和. [输入格式] 第1行:2个空格分开的整数n(2<=n< ...
- 最大m段子段和
hdu1024 最大m子序列和 给定你一个序列,让你求取m个子段(不想交的子段)并求取这m个子段和的最大值 从二维开始来看dp[i][j]表示取第j个数作为第i个子段的元素所得到的前i个子段和的最大值 ...
- MFC中处理UI界面时的注意点
最近开发时,在处理界面上遇到了下面的问题: 上位机与下位机通信时,如果出现超时,弹出MessageBox提示的情况下,更新界面上的CStatic控件会出现重影. 经过调查发现 原因是由于在UI线程中处 ...
- JSON知识介绍
JSON资料整理 目录 1.什么是json 2.json语法规则 3.json基础结构 4.json基础示例 5.JSON和XML比较 6. .NET操作JSON 原始方式 通用方式 内置方式 契 ...
- delphi怎么做桌面滚动文字?
就是在桌面显示从TXT读取出来的字,并限制在1个框内移动(就是从框左边出现往右边移动并从框边消失)我用HDC+textout只是读取字显示到桌面,不知道桌面移动哪位大侠指点下啊,或用其他方法,最好有详 ...
- Win7下无法启动sql server fulltext search (mssqlserver)的问题
在Win7下安装了SQL Server 2005, 但启动“SQL Server FullText Search (MSSQLSERVER)”服务时启动不成功,系统日志显示“SQL Server Fu ...
- ABP 框架 数据库底层迁移 Mysql 集群
技术交流,请加QQ群:538327407 我的各种github 开源项目和代码:https://github.com/linbin524 背景 笔者 目前架构的IOT 项目是使用abp 框架作为后台, ...
- VisualSVN 破解方法
第一步,首先准备反汇编工具 ildasm.exe,找到VisualSVN的安装路径,一般先备份,在备份里面操作. 第二步,转储 , 得到同名的il文件:VisualSVN.Core.L.il,用记事本 ...
- 数据导出之winfrom导出word(二)
本篇文章介绍了根据word模板导出word文档的方法. 一.获取模板地址 WordDocFileHelper WordTem = new WordDocFileHelper(); string pat ...