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. Don't Dismiss Georgia Tech's $6,600 Online Master's Degree

    https://www.pcmag.com/commentary/343924/dont-dismiss-georgia-techs-6-600-online-masters-degree Don't ...

  2. SQL SERVR 逻辑函数

    IIF: 根据布尔表达式计算为 true 还是 false,返回其中一个值. IIF 是一种用于编写 CASE 表达式的快速方法. 它将传递的布尔表达式计算为第一个参数,然后根据计算结果返回其他两个参 ...

  3. PyQt5--QPixmap

    # -*- coding:utf-8 -*- ''' Created on Sep 20, 2018 @author: SaShuangYiBing Comment: ''' import sys f ...

  4. canvas实例_时钟

    效果图:是一个会动的时钟 一.时钟的组成 1.表盘(蓝色)  2.刻度(黑色)  3.时针(黑色)  4.分针(黑色)  5.秒针(红色)需美化     二.主要应用的技术 Canvas画线 Canv ...

  5. Spark2.3文档翻阅的一点简略笔记(WaterMarking)

    写本文原因是之前已经将官网文档阅读过几遍,但是后来工作接触spark机会较少所以没有跟进新特性,利用周末一点闲暇时间粗略阅读一篇,将自己之前遇见过的问题解决过的问题印象不深刻的问题做一下记录. 1关于 ...

  6. 补码与C++的应用

    12.inti=(int)((unsigned int)0xffffffff+(unsigned int)0xffffffff); printf(“%d”,i);结果是:C A.0           ...

  7. python-json模块扩展

    sort_keys=True, indent=4, separators=(',', ': ') 格式 json.dumps(response,sort_keys=True, indent=4, se ...

  8. bip44

    BIP: Layer: Applications Title: Multi-Account Hierarchy for Deterministic Wallets Author: Marek Pala ...

  9. day4-课堂笔记

    变量 成员变量-构造方法里定义 self.xxx 使用:类内部: self.xxx 类外部: 先创建实例 实例.xxx 类变量 类定义下面直接定义 使用:类方法内 cls.xxx 类名.xxx sel ...

  10. Windows7系统下OpenCV2.4.4+PCL1.6.0+SSBA3.0+VS2010 IDE32环境下编译和安装以实现Sfm和PCL点云数据可视化

    最近在学习<深入理解OpenCV:实用计算机视觉项目解析>一书的第三章和第四章时,遇到很多编译问题,书中又没有详细的讲解环境配置和搭建过程.经过多天的捉摸.调试.排错终于将两章的程序都调试 ...