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

  1. PAT 1004 Counting Leaves (30分)

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

  2. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

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

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

  4. PAT甲1004 Counting Leaves【dfs】

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

  5. PAT Advanced 1004 Counting Leaves

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

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

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

  7. 1004. Counting Leaves (30)

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

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

  9. 1004 Counting Leaves (30分) DFS

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

  10. PTA (Advanced Level) 1004 Counting Leaves

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

随机推荐

  1. C/C++控制Windows关机/注销/重启的正确姿势

    简介 说到代码控制Windows关机/注销/重启的方式,有很多种,最简单的不过就是控制命令行,使用system("pause")函数执行一个shutdown -s -t 0,关机就 ...

  2. Oracle 截取字符串(截取固定分隔符中间的字符

    #### Oracle 截取字符串(截取固定分隔符中间的字符) #### ####  oracle 取固定分隔符之间的字符--方法一 substr+ instrSELECT  substr('12JP ...

  3. dns服务器测试工具

    下载地址:https://www.eatm.app/wp-content/uploads/2018/08/eDnsTest.20180810.zip

  4. [CQOI2009]叶子的染色

    传送门:https://www.luogu.org/problemnew/show/P3155 一道挺水的树形dp题,然后我因为一个挺智障的问题debug了一晚上…… 嗯……首先想,如果一个点的颜色和 ...

  5. 【转】头部属性 Meta http-equiv 大全

    http-equiv类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正确和精确地显示网页内容.常用的http-equiv类型有: 1.Content-Type和Content-Langu ...

  6. nmap数据流

    扫描者:1.1.1.1被扫描者:2.2.2.2 0x00 介绍 在日常工作对目标信息收集时,我们经常用到nmap这款网络探测工具和安全/端口扫描器,虽然我们关注的是结果(如目标开启了哪些危险端口,什么 ...

  7. Docker介绍-hc课堂笔记

    1,传统模式-多个服务器:申请.安装jdk等.部署环境. 容器-整包,把有东西打包到一起,把这个包放在服务器上. linux中装了docker,起100个服务,改个数字就可以,5分钟左右. 2,虚拟化 ...

  8. WorldWind源码剖析系列:图层管理器按钮类LayerManagerButton和菜单条类MenuBar

    WorldWindow用户定制控件类中所包含的的可视化子控件主要有:图层管理器按钮类LayerManagerButton和菜单条类MenuBar.BmngLoader类中所包含的的可视化子控件主要有: ...

  9. 【转】系统去掉 Android 4.4.2 的StatusBar和NavigationBar

    系统Hide Status Bar frameworks/base/core/res/res/values/dimens.xml 把  <dimen name="status_bar_ ...

  10. P2962 [USACO09NOV]灯Lights

    贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她,把所 ...