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 ...
随机推荐
- 微信小程序-flex布局中align-items和align-self区别
首先看看菜鸟教程中关于align-items和align-self的定义 align-items:align-items 属性定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式.(对 ...
- java中JDK环境变量的配置
JDK的配置在 window中的配置,我的电脑-->属性-->高级系统设置-->高级-->环境变量中配置,具体下图
- Ubuntu下mount命令的好用处
mount,也就是挂载.如果是让电脑自己挂载Windows的分区,也就是你直接在文件管理器里点击那些Windows的盘符,系统就会帮助你自动挂载,不过其挂载后的名称太长太复杂,不方便终端操作.所以还是 ...
- caffe 每层结构
如何在Caffe中配置每一个层的结构 最近刚在电脑上装好Caffe,由于神经网络中有不同的层结构,不同类型的层又有不同的参数,所有就根据Caffe官网的说明文档做了一个简单的总结. 1. Vision ...
- 使用PerfView监测.NET程序性能(一):Event Trace for Windows
前言: 在日常项目开发中,我们时不时会遇到程序占用了很高CPU的情况,可能是程序里某些未经优化的代码或者Bug,或者是程序运行压力太大.无论是什么原因,我们总希望能看到到底是哪个方法占用了如此高的CP ...
- Python 高级编程 ——观察者模式
观察者模式的定义 :定义了对象之间一对多依赖,当一个对象改变状态时,这个对象的所有依赖者都会收到通知并按照自己的方式进行更新. 按照一个气象站的例子来看观察者模式 从气象站取得数据后要在三个布告牌显示 ...
- WPF自定义Window窗体样式
资源文件代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation ...
- VMware中安装Contos
1 检查BIOS虚拟化支持 2 新建虚拟机 3 新建虚拟机向导 4 创建虚拟空白光盘 5 安装Linux系统对应的CentOS版 6 虚拟机命名和定位磁盘位置 7 处理器配置,看自己是否是双核.多核 ...
- Atom 清空残余数据
rm -rf ~/.atom rm /usr/local/bin/atom rm /usr/local/bin/apm rm ~/Library/Preferences/com.github.atom ...
- Apache VirtualHost的配置
自从电脑更换为mac后, 一直没有时间去配置php的环境.导致每次要更改php代码的时候, 都是本地更改,然后直接推送到服务器上运行 这样的开发和测试及其耗时且繁琐, 所以早上特地决定弄好mac下的p ...