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的更多相关文章

  1. PAT (Advanced Level) 1004. Counting Leaves (30)

    简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  2. 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 ...

  3. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  4. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  5. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  6. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  8. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  9. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

随机推荐

  1. Android 响应menu,back键,点击外部消失

    点击外部消失,只需要设置popupWindow.setBackgroundDrawable(new PaintDrawable()); 设置 popupWindow.setFocusable(true ...

  2. cxgrid动态生成footersummary 并获得值

    cxgrid动态生成footersummary 并获得值   var f: TcxGridDBTableSummaryItem; cx_for_mctv.OptionsView.Footer := t ...

  3. cxGrid控件过滤筛选后如何获更新筛选后的数据集

    cxGrid控件过滤筛选后如何获更新筛选后的数据集 (2015-06-19 12:12:08) 转载▼ 标签: delphi cxgrid筛选数据集 cxgrid过滤 分类: Delphi cxGri ...

  4. 你所不知道的ASP.NET Core MVC/WebApi基础系列 (二)

    转自博客:https://www.cnblogs.com/CreateMyself/p/10604293.html 前言 本节内容,我们来讲讲.NET Core当中的模型绑定系统.模型绑定原理.自定义 ...

  5. 《jQuery基础教程(第四版)》学习笔记

    本书代码参考:Learning jQuery Code Listing Browser 原书: jQuery基础教程 目录: 第2章 选择元素 1. 使用$()函数 2. 选择符 3. DOM遍历方法 ...

  6. EMACS快捷键

    C = Control M = Meta = Alt | Esc Del = Backspace 基本快捷键(Basic) C-x C-f "find"文件, 即在缓冲区打开/新建 ...

  7. Strust2总结

    1. JavaEE软件三层结构和MVC的区别? JavaEE软件三层机构是由sun公司提供JavaEE开发规范的:Web层(表现层).业务逻辑层.数据持久层.[其中WEB层会使用前端控制器模式] MV ...

  8. 201621123018《Java程序设计》第5周学习报告

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口.interface.implements.Comparable.Comparator. 1.2 尝试使用思维导图将这些关键 ...

  9. BZOJ NOIP提高组十连测第一场

    今天的题目一共拿了$180$分,感觉自己还是太菜了,二三两题只能骗到部分分 1.$String\ Master$ 题目大意:有两个字符串,在允许k次失配的情况下,求最长公共子串的长度 没什么好讲,直接 ...

  10. ReactNatvie遇到的错误

    1:新版的React包中没有包含PropTypes,如果使用需要从‘prop-types’包中导入. 2: 'prop-types'包中直接定义‘PropTypes.style’是无效的,需要使用‘P ...