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

1004.计算叶子个数

一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。

输入

每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指树中的结点个数(0<N<100),M指非叶结点的个数。然后下面有M行,每行的格式如下:

ID K ID[1] ID[2] ...ID[K]

ID是一个两位数的数字,表示一个非叶结点。K表示其孩子的数量。随后是一个序列,序列中是该结点的孩子结点的两位数ID。为了简单起见,我们把根结点的ID固定为01。

输出

对于每个测试实例,你应该计算从根结点开始的每一层中没有孩子的家庭成员的个数。数字必须在一行内输出,用空格分隔,在每行结尾不能有多余的空格。

测试样例表示了一个只有两个结点的树,01是根结点,02是它仅有的孩子。因此在根结点01层级,没有叶节点。再下一层级,有一个叶结点。然后我们应该在一行内输出“0 1”。

我用dfs建的树,要注意特殊情况:

0 0 是0

1 0是1

dfs的第4个测试点一直段错误(27/30),求各位大佬帮助,bfs第4个也一直段错误,第2个答案错误(19/30),晕死

  1.  
  2. #include<iostream>
  3. #include<cstring>
  4. #include<string>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<map>
  9. #define inf 0x3f3f3f3f
  10. using namespace std;
  11. queue<int>q[];
  12. int a[];
  13. int rec[];
  14. int n,m;
  15. void build(int root,int k)
  16. {
  17. int count=;
  18. while(!q[k].empty())
  19. {
  20. int x=q[k].front();
  21. q[k].pop();
  22. a[n*root+rec[count]]=x;
  23. if(!q[x].empty())
  24. {
  25. build(n*root+rec[count],x);
  26. }
  27. count++;
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. cin>>n>>m;
  34. for(int i=;i<=n+;i++)
  35. {
  36. rec[i-]=i-n;
  37. }
  38. memset(a,,sizeof(a));
  39. if(n==)
  40. cout<<;
  41. else if(m==)
  42. cout<<;
  43. for(int i=;i<=m;i++)
  44. {
  45. int x;
  46. cin>>x;
  47. int nn;
  48. cin>>nn;
  49. for(int j=;j<=nn;j++)
  50. {
  51. int y;
  52. cin>>y;
  53. q[x].push(y);
  54. }
  55. }
  56. a[]=;
  57. build(,);
  58. int st=,en=;
  59. for(int i=;i<=m*n;i++)
  60. {
  61. int sum=;
  62. bool f=;
  63. for(int j=st;j<=st+en-;j++)
  64. {
  65. if(a[j]!=&&a[n*j+rec[]]==)
  66. sum++;
  67. if(a[j]!=)
  68. f=;
  69. }
  70. if(f==) break;
  71. if(st==)
  72. {
  73. cout<<sum;
  74. }
  75. else
  76. {
  77. cout<<" "<<sum;
  78. }
  79. st=st+en;
  80. en=en*n;
  81. }
  82. return ;
  83. }

bfs代码AC代码

  1. #include<iostream>
  2. #include<cstring>
  3. #include<string>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. #include<map>
  8. #include<vector>
  9. #define inf 0x3f3f3f3f
  10. using namespace std;
  11. int n,m;
  12. vector<int>v[];
  13. queue<int>q;
  14. int main()
  15. {
  16. cin>>n>>m;
  17. for(int i=;i<=m;i++)
  18. {
  19. int x;
  20. cin>>x;
  21. int num;
  22. cin>>num;
  23. for(int j=;j<=num;j++)
  24. {
  25. int y;
  26. cin>>y;
  27. v[x].push_back(y);
  28. }
  29. }
  30. q.push();
  31. if(m==)
  32. cout<<;
  33. else if(n==)
  34. cout<<;
  35. else
  36. {
  37. if(v[].size()==)
  38. {
  39. cout<<;
  40. }
  41. else
  42. cout<<;
  43. int k=;
  44. int kk=;
  45. int nn=;
  46. int count=;
  47. while(!q.empty())
  48. {
  49. if(nn==k)
  50. {
  51. cout<<" "<<count;
  52. count=;
  53. nn=;
  54. k=kk;
  55. kk=;
  56. }
  57. nn++;
  58. int x=q.front();
  59. q.pop();
  60. for(int j=;j<v[x].size();j++)
  61. {
  62. int y=v[x].at(j);
  63. if(v[y].size()==)
  64. count++;
  65. else
  66. {
  67. q.push(y);
  68. kk++;
  69. }
  70.  
  71. }
  72. }
  73. cout<<" "<<count;
  74. }
  75. return ;
  76. }
  1.  

PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)的更多相关文章

  1. 1004 Counting Leaves (30 分)(树的遍历)

    给出一棵树,问每一层各有多少叶子节点 dfs遍历树 #include<bits/stdc++.h> using namespace std; vector<]; int n,m; i ...

  2. 1004 Counting Leaves (30 分)

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

  3. 1004. Counting Leaves(30)—PAT 甲级

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

  4. PTA 1004 Counting Leaves

    题目描述: A family hierarchy is usually presented by a pedigree tree. Your job is to count those family ...

  5. PAT甲1004 Counting Leaves【dfs】

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

  6. 1004 Counting Leaves (30分) DFS

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

  7. PAT 1004 Counting Leaves (30分)

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

  8. 1004. Counting Leaves (30)

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

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

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

随机推荐

  1. python中time()时间的相关问题

    Python中time模块详解(转) 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time ...

  2. C++面向对象高级编程(二)基础篇

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 知识点1.重载成员函数 知识点2 . return by value, return by reference 知识点3 重载非成员函数 ...

  3. L161

    The robot arm made for gentle undersea explorationA soft robotic arm which will allow underwater sea ...

  4. vue.js 源代码学习笔记 ----- observe

    参考 vue 2.2.6版本 /* @flow */ //引入订阅者模式 import Dep from './dep' import { arrayMethods } from './array' ...

  5. asp.net button浏览器端事件和服务器端事件

    OnClientClick:触发浏览器端的响应,OnClick触发服务器端响应; 在服务器aspx.cs脚本中设置按钮属性: this.btnTest.Attributes["OnClick ...

  6. pcm ulaw alaw转换

    static byte ALawCompressTable[] = { 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5 ...

  7. HM编码器代码阅读(1)——介绍以及相关知识

    HM是HEVC(H.265)的开源实现,可以从网上直接下载.HEVC(H.265)是新一代的视频编解码标准.本人目前研究的只是编码器部分,而且还是入门阶段!为了提高自己,边学边记,由于理解不够深入,难 ...

  8. 揭示同步块索引(上):从lock开始

    转自:http://www.cnblogs.com/yuyijq/archive/2009/03/13/1410071.html 大家都知道引用类型对象除实例字段的开销外,还有两个字段的开销:类型指针 ...

  9. {Emgu}{C#}保存图片、视频等

    一.簡介 以前研究所的時候,有使用VC.NET 配合 OpenCV 做影像處理,這東西相當讚,可以省去不少開發時間,今天嘗試看看如何在Visual C# 2008 上使用 OpenCV. 以下引用 O ...

  10. elixir jenkins 集成构建方式配置

    备注:    主要问题是环境变量配置的问题,解决方法是使用软连接进行解决   1. 下载软件包 wget https://github.com/elixir-lang/elixir/releases/ ...