题目链接:https://www.patest.cn/contests/pat-a-practise/1004

大意:输出按层次输出每层无孩子结点的个数

思路:vector存储结点,dfs遍历

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=1e2+;
  8. int n,m,k,x,f[maxn],deep,y;
  9. vector<int> t[maxn];
  10. void init()
  11. {
  12. cin>>n>>m;
  13. while(m--)
  14. {
  15. cin>>x>>k;
  16. while(k--)
  17. {
  18. cin>>y;
  19. t[x].push_back(y);
  20. }
  21. }
  22. }
  23. void dfs(int x,int dep)
  24. {
  25. deep=max(dep,deep);
  26. if(t[x].size()==)f[dep]++;
  27. for(int i=;i<t[x].size();i++)
  28. dfs(t[x][i],dep+);
  29. }
  30.  
  31. int main()
  32. {
  33. init();
  34. dfs(,);
  35. for(int i=;i<=deep;i++)
  36. printf("%s%d",i?" ":"",f[i]);
  37. printf("\n");
  38. return ;
  39. }

PAT A 1004. Counting Leaves (30)【vector+dfs】的更多相关文章

  1. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

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

  2. PAT 1004 Counting Leaves (30分)

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

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

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

  4. 1004. Counting Leaves (30)

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

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

  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【dfs】

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

  8. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  9. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

随机推荐

  1. 【转】Android Support v4、v7、v13的区别和应用场景

    google提供了Android Support Library package 系列的包来保证来高版本sdk开发的向下兼容性,即我们用4.x开发时,在1.6等版本上,可以使用高版本的有些特性,如fr ...

  2. js闭包问题

    function picLinkInit(parentClassName, imgW, imgH, childClassObjs) { var $match = $(parentClassName); ...

  3. codeforces 515A.Drazil and Date 解题报告

    题目链接:http://codeforces.com/problemset/problem/515/A 题目意思:问能否从 (0, 0) 出发,恰好走 s 步,到达该位置(a, b). 首先容易知道, ...

  4. H5 前端页面适配响应式

    辞职有半个月了,面试了几家公司,还在挣扎中.... 不废话,H5页面适配成响应式,可以用百分比或者rem. rem是相对于html根元素的单位,可以根据根元素的大小做出等比缩放, 通常,假如设置,ht ...

  5. suse linux 命令

    1.修改vftpd配置文件   vi /etc/vsftpd .conf                       #listen=YES   vi /etc/xinetd.d/vsftpd     ...

  6. C#实现MD5加密

    摘自:http://blog.csdn.net/shenghui188/archive/2010/03/28/5423959.aspx 方法一 首先,先简单介绍一下MD5 MD5的全称是message ...

  7. Oracle 修改现有列的数据类型

    如果表中有数据,Oracle是不能修改其数据类型的.但可以通过新建一个临时列,将要修改列的数据复制到临时列中,删除原列再修改临时列的名字.这样说好像有点拗口,分步解说一下. 表AC_REG中有列:is ...

  8. 【XLL 框架库函数】 Excel/Excel12f

    Excel/Excel12f 这两个库函数分别包装了 C API 中的 Excel4 和 Excel12 函数,它们会检查函数没有参数时是否为零,它将表明创建临时的 XLOPER 或 XLOPER12 ...

  9. UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

    Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...

  10. 二维码相关工具Qrcode笔记