简单DFS。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstdio>
  6. #include<vector>
  7. using namespace std;
  8.  
  9. const int maxn=+;
  10. vector<int>g[maxn];
  11. int n,m;
  12. int ans[maxn];
  13. int root;
  14. int Deep;
  15.  
  16. void dfs(int x,int deep)
  17. {
  18. Deep=max(Deep,deep);
  19. if(g[x].size()==)
  20. {
  21. ans[deep]++;
  22. return ;
  23. }
  24. for(int i=;i<g[x].size();i++)
  25. dfs(g[x][i],deep+);
  26. }
  27.  
  28. int main()
  29. {
  30. scanf("%d%d",&n,&m);
  31. for(int i=;i<=n;i++) g[i].clear();
  32. for(int i=;i<=m;i++)
  33. {
  34. int id; scanf("%d",&id);
  35. int k; scanf("%d",&k);
  36. while(k--)
  37. {
  38. int id2; scanf("%d",&id2);
  39. g[id].push_back(id2);
  40. }
  41. }
  42. memset(ans,,sizeof ans);
  43. root=; Deep=;
  44. dfs(root,);
  45. for(int i=;i<=Deep;i++)
  46. {
  47. printf("%d",ans[i]);
  48. if(i<Deep) printf(" ");
  49. else printf("\n");
  50. }
  51. return ;
  52. }

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

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

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

  2. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  3. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

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

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

  5. PAT 1004 Counting Leaves (30分)

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

  6. 1004. Counting Leaves (30)

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

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

  8. 1004 Counting Leaves (30分) DFS

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

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

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

随机推荐

  1. Cowrie蜜罐部署教程【转载】

    0.蜜罐分类: 低交互:模拟服务和漏洞以便收集信息和恶意软件,但是攻击者无法和该系统进行交互: 中等交互:在一个特有的控制环境中模拟一个生产服务,允许攻击者的部分交互: 高交互:攻击者可以几乎自由的访 ...

  2. js实现td排序及分组分类

    如题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  3. js css优化-- 合并和压缩

    在项目框架中,首先要引用很多css和js文件,80%的用户响应时间都是浪费在前端.而这些时间主要又是因为下载图片.样式表.JavaScript脚本.flash等文件造成的.减少这些资源文件的Reque ...

  4. MyBatis-执行插入语句的时候返回主键ID到传入的参数对象中

    <!-- 保存项目信息 --> <insert id="saveItem" parameterType="pd" useGeneratedKe ...

  5. php 四种基础算法 ---- 选择排序法

    2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...

  6. jQuery Mobile 学习

    jQuery Mobile 学习系列 http://blog.csdn.net/bao990423420/article/details/13995021

  7. Android内核驱动程序的编写和编译过程

    注意:涉及的代码为android内核代码而不是android源码. 在智能手机时代,每个品牌的手机都有自己的个性特点.正是依靠这种与众不同的个性来吸引用户,营造品牌凝聚力和用户忠城度,典型的代表非ip ...

  8. Python实现删除目录下相同文件

    让我们来分析一下这个问题:首先,文件个数非常多,手工查找是不现实的,再说,单凭我们肉眼,在几千张图片或文件里面找到完全相同的难度也是很大的.所以要用程序实现.那么用程序怎么实现呢?根据什么判断两个文件 ...

  9. div里面的margin-top失效

    div标签中的元素margin-top失效的解决方法 元素上级标签是div,已经设置了width和height等的属性,可是,在对元素使用margin进行调整的时候,无法生效,下面有个不错的解决方法, ...

  10. html 7.29

    4.请判断以下说法是否正确:HTML 会被 XHTML 取代. 您的回答:错误 正确答案:正确 9.请判断以下说法是否正确:DOCTYPE 没有关闭标签. 您的回答:错误 正确答案:正确 13.下列哪 ...