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, the number of nodes in a tree, and M (<), 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 01level, 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int tree[][] = { -1 };//只会赋给第一个元素,别的默认为0
//fill(tree[0], tree[0] + 101*101, -1);
int leaf[] = { };
int step = ;
void dfs(int i, int depth){
if (tree[i][] == -1){
leaf[depth]++;
return;
}
int j = ;
while (tree[i][j] != -1){
dfs(tree[i][j], depth + );
j++;
}
step = max(step, depth + );
}
int main(){
int n, m;
fill(tree[0], tree[0] + 101*101, -1);
cin >> n >> m;
for (int i = ; i < m; i++){
int root, num;
cin >> root >> num;
for (int j = ; j < num; j++){
int leaf;
cin >> leaf;
tree[root][j] = leaf;
}
}
int root = , depth = ;
dfs(root, depth);
for (int i = ; i <= step; i++){
if(i!=step)cout << leaf[i] << " ";
else cout << leaf[i];
}
system("pause");
}

注意点:一开始题目都没看懂,给的例子太不具代表性了,读了好多遍终于知道是要求每层的叶节点个数并输出。就是考了一个深度优先搜索(DFS),居然还不会做,DFS要用递归一层层遍历,遍历完要记录最深到几层了,方便后面输出。这里建树用了二维数组,其实有点蠢,用vector数组会方便一点。另外,二维数组的初始化只能用fill或memset,直接像一维数组那样给一个值只会赋给数组第一个元素。

PAT A1004 Counting Leaves (30 分)——树,DFS,BFS的更多相关文章

  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. 1004 Counting Leaves (30分) DFS

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

  3. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  4. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

    题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...

  5. PAT 1004. Counting Leaves (30)

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

  6. 1004 Counting Leaves (30 分)

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

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

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

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

  9. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

随机推荐

  1. 使用git将本地仓库同步到github远程仓库

    一.下载安装git客户端windows版本 二.建立本地仓库文件夹 三.在本地仓库里右键点击Git Bash Here 四.初始化本地仓库 [git init] 执行之后仓库中会创建隐藏的文件夹.gi ...

  2. php+xml有什么用

    很多招聘网上找php程序员的时候都说要懂xml,这个xml+php在web网站开发方面到底有什么应用呢,希望有知道的朋友能给我具体说说,谢谢了! 我说的是在网站中的实际应用有哪些,不是网上抄的xml的 ...

  3. JavaScript:作用域与作用域链

    1.什么是作用域(scope)? 简单来讲,作用域(scope)就是变量访问规则的有效范围. 作用域外,无法引用作用域内的变量: 离开作用域后,作用域的变量的内存空间会被清除,比如执行完函数或者关闭浏 ...

  4. 【读书笔记】iOS-网络-应用间通信

    一,URL方案 URL方案有3个主要用途:根据设备上其他应用的存在与否调整逻辑,切换到其他应用以及响应打开你的应用的其他应用.你还可以通过URL方案从某个站点或是在基于Web的认证流程结束是打开应用. ...

  5. css3之transform属性实现div不定宽高垂直水平居中

    transform的作用 transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜.(w3cschool) transform的兼容性 transfor ...

  6. 2018-10-18 22:15:32 c language

    2018-10-18 22:15:32 c language 在屏幕上输出各种类型的数据 我们使用 puts 来输出字符串.puts 是 output string 的缩写,只能用来输出字符串,不能输 ...

  7. React Native - TextInput详细解说

    1,TextInput组件介绍 TextInput 组件除了作为输入框实现基本的输入功能外,它还提供了许多其他功能,比如自动校验.占位符以及指定弹出不同的键盘类型等. 2,组件的属性 (1)autoC ...

  8. MySQL——通过EXPLAIN分析SQL的执行计划

    在MySQL中,我们可以通过EXPLAIN命令获取MySQL如何执行SELECT语句的信息,包括在SELECT语句执行过程中表如何连接和连接的顺序. 下面分别对EXPLAIN命令结果的每一列进行说明: ...

  9. Problem4-Project Euler

    Largest palindrome product   A palindromic number reads the same both ways. The largest palindrome m ...

  10. VMWare12虚拟机实现主客机间的文件拖拽(复制粘贴)和文件夹共享

    版本: 主机:Windows 7 64位旗舰版 虚拟机: VMWare 12 + Windows 7 64位旗舰版 VMWare pro 12 + Ubuntu16.04LTS 64位 注:由于VMW ...