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:

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

  1. 2 1
  2. 01 1 02

Sample Output:

  1. 0 1
  2.  
  3. 即遍历整颗数,使用DFS或者DFS
    用数组记录每个节点的子节点是谁
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. //给出一棵树,问每一层的叶子结点数量
  8. //使用BFS或者DFS
  9.  
  10. vector<vector<int>>nodes();
  11. vector<int>depth();
  12. int maxDepth = -;
  13.  
  14. void DFS(int index, int h)
  15. {
  16. maxDepth = maxDepth > h ? maxDepth : h;
  17. if (nodes[index].size() == )//data[index].size() == 0)//即为叶子结点
  18. depth[h]++;//层数
  19.  
  20. for (int i = ; i < nodes[index].size(); ++i)
  21. DFS(nodes[index][i], h + );
  22. }
  23.  
  24. void BFS( )
  25. {
  26. queue<int>q;
  27. q.push();
  28. vector<int>level(, );//记录节点层数
  29. while (!q.empty())
  30. {
  31. int index = q.front();
  32. q.pop();
  33. maxDepth = maxDepth > level[index] ? maxDepth : level[index];//存储最大的层数
  34. if (nodes[index].size() == )//此节点为叶子节点
  35. depth[level[index]]++;//之所以要记录每个节点的层数,是因为,同一层有多个节点
  36. for (int i = ; i < nodes[index].size(); ++i)
  37. {
  38. level[nodes[index][i]] = level[index] + ;//孩子结点层数比父节点多一层
  39. q.push(nodes[index][i]);//将其孩子全部存入
  40. }
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. int N, M;//N为节点数目
  47. cin >> N >> M;
  48. for (int i = ; i < M; ++i)
  49. {
  50. int ID, k, a;
  51. cin >> ID >> k;
  52. for (int j = ; j < k; ++j)
  53. {
  54. cin >> a;
  55. nodes[ID].push_back(a);//即为一个节点底下所挂的子节点
  56. }
  57. }
  58.  
  59. //DFS(1,0);
  60. BFS( );
  61. cout << depth[];
  62. for (int i = ; i <= maxDepth; ++i)
  63. cout << " " << depth[i];
  64. cout << endl;
  65.  
  66. return ;
  67.  
  68. }

PAT甲级——A1004 Counting Leaves的更多相关文章

  1. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  2. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  3. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...

  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. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

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

  7. pat 甲级 1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...

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

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

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

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

随机推荐

  1. .NET Core 3.0之深入源码理解Startup的注册及运行

    原文:.NET Core 3.0之深入源码理解Startup的注册及运行   写在前面 开发.NET Core应用,直接映入眼帘的就是Startup类和Program类,它们是.NET Core应用程 ...

  2. hadoop系列(一)window10下hadoop安装

    风闻win10不需要cygwin就能用hadoop了,赶紧试试. 去官网下载hadoop-2.8.3,然后去 https://github.com/steveloughran/winutils 下载h ...

  3. websokect的原理

    WebSocket 机制 以下简要介绍一下WebSocket的原理及运行机制. WebSocket是HTML5下一种新的协议.它实现了浏览器与服务器全双工通信,能更好的节省服务器资源和带宽并达到实时通 ...

  4. Two-phase Termination 把玩具收拾好再去睡觉。

    字面翻译是“两阶段终止”,这个模式用来进行结束操作后,再终止线程.比如我们想停止一个线程,但是让他停止之前必须要做一些清理工作,这时候就需要用到two-phase termination模式. pub ...

  5. Java中的API方法总结

    API方法总结 File file = new File(path); #创建文件对象,指向一个目录 file.exists() #判断目录或者文件是否存在 File[] files = file.l ...

  6. SpringBoot 非web项目简单架构

    1.截图 2.DemoService package com.github.weiwei02.springcloudtaskdemo; import org.springframework.beans ...

  7. MySQL架构和索引

    MySQL架构 逻辑架构图: 大概分为四层,这个见仁见义,有不同的分法: 第一层Connectors:处理不同语言与SQL的交互 第二层Connection Pool :连接池,管理缓存用户连接,线程 ...

  8. PHP MVC运用

    php中的MVC模式运用 首先我来举个例子: 一个简单的文章显示系统 简单期间,我们假定这个文章系统是只读的,也就是说这个例子将不涉及文章的发布,现在开始了. 由于只涉及数据库的读取,所以我定义了两个 ...

  9. python处理多线程之间事件通讯方法

    一.什么是事件 每执行一个事情,肯定有该事情的执行后状态,那事件就是该事情发生的信号 在程序中,多线程之间需要通讯,而事件就是方便线程之间的通讯 案例: 1.服务器启动需要5秒 2.客服端启动后去链接 ...

  10. react 组件的生命周期 超简版

    组件从被创建到被销毁的过程称为组件的 生命周期: 通常,组件的生命周期可以被分为三个阶段:挂载阶段.更新阶段.卸载阶段: 一.挂载阶段 这个阶段组件被创建,执行初始化,并被挂载到DOM中,完成组件的第 ...