利用广度优先搜索,找出每层的叶子节点的个数。

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. vector<vector<int>> tree;
  8. vector<int> ans;
  9.  
  10. void BFS(int s)
  11. {
  12. queue<pair<int, int>> q;
  13. q.push(make_pair(s, 0));
  14. int cur_step = 0;
  15. int cnt = 0;
  16.  
  17. while (!q.empty())
  18. {
  19. int id = q.front().first;
  20. int step = q.front().second;
  21. q.pop();
  22. if(step > cur_step)
  23. {
  24. cur_step = step;
  25. ans.push_back(cnt);
  26. cnt = 0;
  27. }
  28. if(tree[id].size() == 0) cnt++;
  29. for(int i = 0; i < tree[id].size(); i++)
  30. q.push(make_pair(tree[id][i], step + 1));
  31. }
  32. ans.push_back(cnt);
  33. return;
  34. }
  35.  
  36. int main()
  37. {
  38. //fstream cin("a.txt");
  39.  
  40. int N, M;
  41. cin>>N>>M;
  42. tree.resize(N + 1);
  43. for(int i = 0; i < M; i++)
  44. {
  45. int id, num;
  46. cin>>id>>num;
  47. while (num--)
  48. {
  49. int tmp;
  50. cin>>tmp;
  51. tree[id].push_back(tmp);
  52. }
  53. }
  54. BFS(1);
  55. for(int i = 0; i < ans.size(); i++)
  56. {
  57. if(i == 0)
  58. cout<<ans[i];
  59. else
  60. cout<<" "<<ans[i];
  61. }
  62. cout<<endl;
  63. }

【PAT Advanced Level】1004. Counting Leaves (30)的更多相关文章

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

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

  2. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

  3. 【PAT Advanced Level】1006. Sign In and Sign Out (25)

    关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...

  4. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  5. 【PAT Advanced Level】1015. Reversible Primes (20)

    转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...

  6. 【PAT Advanced Level】1011. World Cup Betting (20)

    简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...

  7. 【PAT Advanced Level】1013. Battle Over Cities (25)

    这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...

  8. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

  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. 【mysql的设计与优化专题(2)】数据中设计中的范式与反范式

    设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小.但是有些时候一昧的追求范式减少冗余,反而会降低数据读写 ...

  2. laravel Authentication and Security

    Creating the user modelFirst of all, we need to define the model that is going to be used to represe ...

  3. JavaScript DOM高级程序设计 5动态修改样式和层叠样式表1(源代码)--我要坚持到底!

    W3C DOM2样式规范 现在这边贴出本章要的源代码,注意要结合前面用到的ADS库http://vdisk.weibo.com/s/Dq8NU CSSStyleSheet对象属性: type :始终是 ...

  4. Weak Event Patterns

    https://msdn.microsoft.com/en-US/library/aa970850(v=vs.100).aspx In applications, it is possible tha ...

  5. C语言中指针数组和数组指针的区别

    指针数组:首先它是一个数组,数组的元素都是指针,数组占多少个字节由数组本身决定.它是“储存指针的数组”的简称. 数组指针:首先它是一个指针,它指向一个数组.在32 位系统下永远是占4 个字节,至于它指 ...

  6. naotu.baidu.com 非常棒的脑图在线工具

    1.png 2.txt 短租 前台功能 房源查看 房源搜索 城市房源 注册登录 预定房源 房源退订 在线支付 评价房源 个人中心 我的订单 我的账户 我的收藏 消息通知 管理员后台 房源发布 会员管理 ...

  7. innodb 悲观锁,乐观锁

    转 http://www.cnblogs.com/chenwenbiao/archive/2012/06/06/2537508.html CREATE TABLE `products` ( `id` ...

  8. windows8安装xna4.0不能开发Xbox和PC端游戏的解决办法

    vs2012安装wp8后,只能开发手机端的xna游戏程序,没有xbox和pc端的,看来官方是不打算更新了,不过我们还是有办法的. 前提条件下,您得安装了vs2010和xna4.0 game studi ...

  9. 在线API,桌面版,jquery,css,Android中文开发文档,JScript,SQL掌用实例

    学习帮助文档大全 jquery,css,Android中文开发文档,JScript,SQL掌用实例 http://api.jq-school.com/

  10. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9

    (1). When $A$ is normal, the set $W(A)$ is the convex hull of the eigenvalues of $A$. For nonnormal ...