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.  
  3. 有关树的遍历的问题,用到了深搜的方法
    树的存放很有意思,map<int,vector<int> >; key为某个节点的IDvector是他的孩子ID数组
  4.  
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. map<int,vector<int> > adjlist;
  8. int levelleaves[]={};
  9.  
  10. void dfs(int node,int level){
  11. if(adjlist[node].empty()){
  12. levelleaves[level]++;
  13. return;
  14. }
  15. vector<int>:: iterator itea = adjlist[node].begin();
  16. for(;itea!=adjlist[node].end();itea++){
  17. dfs(*itea,level+);
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. int n,m;
  24. cin>>n>>m;
  25. int leaves = n-m;
  26. for(int i=;i<m;i++){
  27. int id1,k, id2;
  28. cin>>id1>>k;
  29. for(int j=;j<k;j++){
  30. cin>>id2;
  31. adjlist[id1].push_back(id2);
  32. }
  33. }
  34. dfs(,);
  35. int a=levelleaves[];
  36. cout<<levelleaves[];
  37. for(int i=;a<leaves;i++){
  38. cout<<" "<<levelleaves[i];
  39. a=a+levelleaves[i];
  40. }
  41. return ;
  42. }
  1.  
  2.  

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 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

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

  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分) DFS

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

  5. PAT 1004. Counting Leaves (30)

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

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

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

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

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

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

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

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

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

随机推荐

  1. VSS转SVN

    我们都知道VSS和SVN都是源代码的版本控制软件:最近公司准备把多年使用的VSS代码全部转到SVN中进行管理,查询了一些资料,整理一下,分享给大家 基本分三大步进行,如下: 1.去掉VSS所有绑定 2 ...

  2. VR原理讲解及开发入门

    本文是作者obuil根据多年心得专门为想要入门的VR开发者所写,由52VR网站提供支持.   1. VR沉浸感和交互作用产生的原理:   在之前,我们观看一个虚拟的创造内容是通过平面显示器的,52VR ...

  3. Leetcode: Minimum Unique Word Abbreviation

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  4. Babel下的ES6兼容性与规范

    前端开发 Babel下的ES6兼容性与规范   ES6标准发布后,前端人员也开发渐渐了解到了es6,但是由于兼容性的问题,仍然没有得到广泛的推广,不过业界也用了一些折中性的方案来解决兼容性和开发体系问 ...

  5. HTML5音频视频-视频播放

  6. nodejs require//////////z

    背景 这篇文基本都是反对的,反对的很有道理,不是说我这篇文章的内容错误,因为这篇文章是我在健身房学习node的时候写的,这些知识都很粗糙,后来发现官方的稳定更详细:地址:http://nodejs.o ...

  7. 我的第二个FluentNHibernate例子with Knockout

    在上一篇我的第一个FluentNHibernate例子的基础上,我们用上knockoutjs 1用nuget添加knockoutjs包 2用nuget添加json.net包 3..在Index.csh ...

  8. tomcat 开机后台运行

    引用:http://jingyan.baidu.com/article/a65957f4b12b8724e77f9b5a.html Tomcat是Apache 软件基金会(Apache Softwar ...

  9. webpack模块依赖管理介绍

    http://webpack.github.io/docs/ webpack is a module bundler. 是一个模块管理器 webpack可以管理模块的依赖关系,并产生可以替代这些模块的 ...

  10. 浅谈sql的字符分割

    对于oracle:在字符串处理时:经常会遇到字符串分割的问题:可惜SQL中没有split函数:这个倒是挺困扰我们写sql的.对此:我来说说这字符串分割. 例如对字段str中一条数据是'120-mm-2 ...