题目

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

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.

Output

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

题目分析

已知每个节点的子节点,统计每层叶子节点数

解题思路

思路 01

  1. dfs深度优先遍历树,h记录当前节点所在层数,max_h记录最大层数,int left[n]记录每层叶子节点数

思路 02

  1. bfs广度优先遍历树(借助queue),max_h记录最大层数,int left[n]记录每层叶子节点数,int h[n]记录每个节点所在层数(根节点初始化为h[1]=0)

知识点

  1. 广度优先遍历树,使用int h[n]记录每个节点所在层数
  2. 深度优先遍历树,使用参数h记录当前节点所在层数

Code

Code 01(dfs)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> nds[101];
struct node {
int data;
int depth=0;
};
int max_h,leaf[101];
void dfs(int index,int h) {
max_h=max(h,max_h);//记录最大层数
if(nds[index].size()==0) { //叶子节点
leaf[h]++;
return;
}
for(int i=0; i<nds[index].size(); i++) {
dfs(nds[index][i],h+1);
}
}
int main(int argc, char * argv[]) {
int n,m,rid,cid,k;
scanf("%d %d",&n,&m);
for(int i=0; i<m; i++) {
scanf("%d %d",&rid,&k);
for(int j=0; j<k; j++) {
scanf("%d",&cid);
nds[rid].push_back(cid);
}
}
dfs(1,0);
for(int i=0; i<=max_h; i++) {
if(i!=0)printf(" ");
printf("%d",leaf[i]);
}
return 0;
}

Code 02(bfs)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> nds[101];
int leaf[101],h[101],max_h;
void bfs(){
queue<int> q;
q.push(1);
while(!q.empty()){
int now = q.front();
q.pop();
max_h=max(max_h,h[now]);
if(nds[now].size()==0){
leaf[h[now]]++;
} else{
for(int i=0;i<nds[now].size();i++){
h[nds[now][i]]=h[now]+1;
q.push(nds[now][i]);
}
}
}
}
int main(int argc, char * argv[]) {
int n,m,rid,cid,k;
scanf("%d %d",&n,&m);
for(int i=0; i<m; i++) {
scanf("%d %d",&rid,&k);
for(int j=0; j<k; j++) {
scanf("%d",&cid);
nds[rid].push_back(cid);
}
}
h[1]=0;
bfs();
for(int i=0;i<=max_h;i++){
if(i!=0)printf(" ");
printf("%d",leaf[i]);
}
return 0;
}

PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]的更多相关文章

  1. 1004 Counting Leaves (30分) DFS

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

  2. PAT Advanced 1004 Counting Leaves

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

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

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

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

  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 (30分)

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

  7. 1004. Counting Leaves (30)

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

  8. PAT甲1004 Counting Leaves【dfs】

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

  9. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

随机推荐

  1. win上java1.7和1.8版本修改环境变量无效.md

    网上找了很多办法都没用. 解决办法: 看看自己 "系统环境变量" 中是不是有 "C:\ProgramData\Oracle\Java\javapath" 这项配 ...

  2. git commit -m 和 git commit -am 区别

    git commit -m 和 git commit -am 通常修改一个文件 并且将文件提交到本地分支的命令是: git add . git commit -m 'update' 以上两个命令其实可 ...

  3. TS文件极简合并

    TS文件是可以直接通过二进制拷贝连接的方式进行合并的,一般采用如下的命令行参数:copy /b 1.ts+2.ts+3.ts new.ts 这个例子就是将1.ts.2.ts.3.ts三个文件按顺序连接 ...

  4. 自制spring中bean加载机制,仅做笔记自用

  5. JS的数据类型、常量、变量、以及基本对象的知识总结

    第一部分.JS的常见数据类型,特别要注意的是JS中大小写要求很严格,一定要注意字段大小写. 1.字符串(String) 举例: var cellname = "Bill Gate" ...

  6. Photoshop 更换证件照底色

    1.打开photoshop CS6. 2.打开照片 (上栏)文件---->打开 或者,直接把照片拖进ps中.  3.复制图层 右击背景,点击复制图层 (复制图层的作用是,如果对图层操作错误,可以 ...

  7. [APIO2012]派遣 可并堆

    Background 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿. Description 在这个帮派里,有一名忍者被称之为Master.除了Master以外,每名忍者 ...

  8. 浅谈Python之sys.argv

    (1)sys.argv是什么 sys模块为进入解释器维护或使用的变量,以及与解释器相关的函数提供了途径.sys.argv在脚本程序中扮演了这样一个角色:将命令行输入的参数作为一个list传入脚本程序, ...

  9. UVA - 11134 Fabled Rooks(传说中的车)(贪心)

    题意:在n*n的棋盘上放n个车,使得任意两个车不相互攻击,且第i个车在一个给定的矩形Ri之内,不相互攻击是指不同行不同列,无解输出IMPOSSIBLE,否则分别输出第1,2,……,n个车的坐标. 分析 ...

  10. tensorflow--建立一个简单的小网络

    In [19]:           import tensorflow as tf import numpy as np # #简单的数据形网络 # #定义输入参数 # X=tf.constant( ...