题目

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)

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. vector<int> nds[101];
  6. struct node {
  7. int data;
  8. int depth=0;
  9. };
  10. int max_h,leaf[101];
  11. void dfs(int index,int h) {
  12. max_h=max(h,max_h);//记录最大层数
  13. if(nds[index].size()==0) { //叶子节点
  14. leaf[h]++;
  15. return;
  16. }
  17. for(int i=0; i<nds[index].size(); i++) {
  18. dfs(nds[index][i],h+1);
  19. }
  20. }
  21. int main(int argc, char * argv[]) {
  22. int n,m,rid,cid,k;
  23. scanf("%d %d",&n,&m);
  24. for(int i=0; i<m; i++) {
  25. scanf("%d %d",&rid,&k);
  26. for(int j=0; j<k; j++) {
  27. scanf("%d",&cid);
  28. nds[rid].push_back(cid);
  29. }
  30. }
  31. dfs(1,0);
  32. for(int i=0; i<=max_h; i++) {
  33. if(i!=0)printf(" ");
  34. printf("%d",leaf[i]);
  35. }
  36. return 0;
  37. }

Code 02(bfs)

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. vector<int> nds[101];
  6. int leaf[101],h[101],max_h;
  7. void bfs(){
  8. queue<int> q;
  9. q.push(1);
  10. while(!q.empty()){
  11. int now = q.front();
  12. q.pop();
  13. max_h=max(max_h,h[now]);
  14. if(nds[now].size()==0){
  15. leaf[h[now]]++;
  16. } else{
  17. for(int i=0;i<nds[now].size();i++){
  18. h[nds[now][i]]=h[now]+1;
  19. q.push(nds[now][i]);
  20. }
  21. }
  22. }
  23. }
  24. int main(int argc, char * argv[]) {
  25. int n,m,rid,cid,k;
  26. scanf("%d %d",&n,&m);
  27. for(int i=0; i<m; i++) {
  28. scanf("%d %d",&rid,&k);
  29. for(int j=0; j<k; j++) {
  30. scanf("%d",&cid);
  31. nds[rid].push_back(cid);
  32. }
  33. }
  34. h[1]=0;
  35. bfs();
  36. for(int i=0;i<=max_h;i++){
  37. if(i!=0)printf(" ");
  38. printf("%d",leaf[i]);
  39. }
  40. return 0;
  41. }

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. wincc的服务器-客户机模式具体做法(全抄-未测试)

    一.原来的工作方式:在同一工作组中4台计算机其windows名分别为A.B.C.D且都已安装好wincc5.0+sp2,原来在每台计算机上运行的均是单用户,4台计算机上实际运行的是一个相同的项目,最先 ...

  2. 吴裕雄--天生自然java开发常用类库学习笔记:Stack类

    import java.util.Stack ; public class StackDemo{ public static void main(String args[]){ Stack<St ...

  3. JuJu团队11月27号工作汇报

    JuJu团队11月27号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 于达 将真实数据处理后按矩阵读入, 以供训练使用  提供generator的接口 对julia语言还不够 ...

  4. ROS大型工程学习(二) 怎么阅读大型工程

    基本思路是由点到面,由浅到深. 1.首先从launch文件入手. 文件中会看到比如: <node ns="> <rosparam command="load&qu ...

  5. 启用root关闭客人会话

    1.位root用户设置密码: sudo passwd root 2.修改配置文件/usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf(先备份) 添加如下在文 ...

  6. 提交作业 C语言I作业11

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 http://edu.cnblogs.com/campus/zswxy/SE2019-2/homework/10127 我在这个课程的目标 ...

  7. 那些年我们一起踩过的坑(javascript常见的陷阱)

    1.object最后一个逗号 定义object直接量或json,最后一个逗号多写了,在ie下会报错,高级浏览器则不会,给只使用chrome调试的同学敲个警钟.踩了无数次这个坑了.   2.自动加分号 ...

  8. log4j 打印日志

    # Set log levels #     设置日志级别 log4j.rootLogger = INFO, DebugFile,Console, LogFile, ErrorFile ## Disa ...

  9. ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建

    ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建 集群规划: hostname NameNode DataNode JournalNode Re ...

  10. oracle中判断"非"

    在oracle中判断为"非"最常见的两种情况,一个是"不等于",一个的"非空". 通过查找资料得知,oracle中判断不等于的方法有好多种: ...