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>**

最新文章

  1. 125个工具与技术(PMBOK2008)
  2. js中遍历对象的属性和值
  3. php中set_time_limit()函数运用
  4. oslo.messaging 1.8.0 bug fix and blueprint
  5. 创建线程方式-pthread
  6. Shell基础整理
  7. JavaScript获取DOM元素位置和尺寸大小
  8. ylb:SQL Server中的时间函数
  9. [转]使用 PIVOT 和 UNPIVOT
  10. php的标记形式
  11. ASP.NET实现IE下禁用浏览器后退按钮办法
  12. Qt :非window子窗体的透明度设置
  13. Python 数据分析包:pandas 基础
  14. java注解编程
  15. C++教程之autokeyword的使用
  16. MyEclipse过期后怎么破解
  17. anndroid 模糊引导界面
  18. Oracle集群时区
  19. [STM32F103]RTC日历
  20. 【Spring】—— 自动装配

热门文章

  1. 【待补充】Spark 集群模式 &amp;&amp; Spark Job 部署模式
  2. Oracle 截取字符串(截取固定分隔符中间的字符
  3. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
  4. Python返回数组(List)长度的方法
  5. python difflib.md
  6. myEtherWallet在线钱包的使用
  7. android camera 摄像头预览画面变形
  8. Jenkins Extended E-mail Notification 2个注意事项:
  9. LFS 8.3 中文翻译版本发布!
  10. filebeat配置