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

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:

  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.

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

  1. 2 1
  2. 01 1 02

Sample Output

  1. 0 1
  2.  

题目描述:

统计一颗树每一层的leaf数量。

算法分析:

思路1:BFS

本质就是lever order traversal, 可以用bfs遍历,然后每一层统计叶子数。

思路2:DFS

可以使用邻接矩阵的方式定义树结构。然后使用 dfs 遍历树的节点,并记录每层的叶子节点数量。 可以看到,时间空间的 trade-off 不仅仅是性能上的提升,也会影响带代码实现的复杂程度。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. #define MX 101
  10.  
  11. int mp[MX][MX];
  12. queue<int> que;
  13. int n,m;
  14.  
  15. int bfs(int s) {
  16. int flag = ;
  17. for (int i=; i<=n; i++) {
  18. if (mp[s][i] == ) {
  19. que.push(i);
  20. flag = ;
  21. }
  22. }
  23. return flag;
  24. }
  25.  
  26. void actbfs() {
  27. que.push();
  28. que.push();
  29. int cnt = ;
  30. while (!que.empty()) {
  31. int s = que.front();
  32. que.pop();
  33. if (s == ) {
  34. if (que.empty()) {
  35. printf("%d", cnt);
  36. break;
  37. }
  38. else {
  39. que.push();
  40. printf("%d ", cnt);
  41. cnt = ;
  42. }
  43. }
  44. else {
  45. int flag = bfs(s);
  46. cnt += flag;
  47. }
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. scanf("%d%d", &n,&m);
  54. memset(mp, , sizeof(mp));
  55. for (int i=; i<m; i++) {
  56. int id,k;
  57. scanf("%d%d", &id,&k);
  58. for (int j=; j<k; j++) {
  59. int chi;
  60. scanf("%d", &chi);
  61. mp[id][chi] = ;
  62. }
  63. }
  64.  
  65. actbfs();
  66.  
  67. return ;
  68. }
  1.  

PAT 解题报告 1004. Counting Leaves (30)的更多相关文章

  1. PAT 解题报告 1049. Counting Ones (30)

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

  2. PAT (Advanced Level) 1004. Counting Leaves (30)

    简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

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

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

  4. PAT 1004 Counting Leaves (30分)

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

  5. 1004. Counting Leaves (30)

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

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

  7. 1004 Counting Leaves (30分) DFS

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

  8. PAT 1004. Counting Leaves (30)

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

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

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

随机推荐

  1. 【IOS笔记】View Programming Guide for iOS -1

    原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...

  2. P1541 乌龟棋

    30分做法,暴力枚举: #include <bits/stdc++.h> using namespace std; const int maxn = 400; int n, m; int ...

  3. C++ - 扩展欧几里德算法非递归实现

    #include <iostream> using namespace std; int x, y; void get_x_y(int a, int b){ int q, r[3], s[ ...

  4. duplicate symbol _OBJC_CLASS 错误处理方法

    错误: ld: duplicate symbol _OBJC_CLASS_$_************ in **************** 一种可能性是你的项目的不同group里有着相同名称的类 ...

  5. struts ActionContext ThreadLocal

    public class ActionContext implements Serializable The ActionContext is the context in which an Acti ...

  6. 文件批量上传的工具,要实现暂停继续、断点续传等功能(使用QtNetwork和QHttpMultiPart,和定时器检查超时)

    最近在做一个文件批量上传的工具,要实现暂停继续.断点续传等功能.利用Qt自带的QtNetwork模块,完成这些需求并没有费多少周章,主要思路就是将文件分块,然后用while循环依次传输.具体实现代码比 ...

  7. jquery.autocomplete.js 两种实现方法

    <script type="text/javascript"> var v = 1; var stockInfoJson = [ { "name": ...

  8. ASP.NET中身份验证

    ASP.NET中身份验证有三种方式:Windows.Forms和Passport. 1.Windows验证,基于窗体验证,需要每个页面写上验证身份代码,相对灵活,但操作过于复杂: 2.Passport ...

  9. SEO优化笔记

    1,清理垃圾代码. 清理垃圾代码是指删除页面中的冗余代码,可以删除80%的冗余代码,垃圾代码主要指那些删除了也不会对页面有任何影响的非必要代码.最常见的垃圾代码:空格空格字符是网页中最常见的垃圾代码. ...

  10. ThreadLocal 多线程并发,数据隔离

    ThreadLocal:  创建一个线程本地变量. 本质:在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本. 优点:既实现多线程并发,游兼顾数据的安全性. 区别:Synchro ...