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:

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

2 1
01 1 02

Sample Output

0 1

有关树的遍历的问题,用到了深搜的方法
树的存放很有意思,map<int,vector<int> >; key为某个节点的ID,vector是他的孩子ID数组
#include <iostream>
#include <map>
#include <vector> using namespace std; map<int,vector<int> > adjlist;
int levelleaves[]={}; void dfs(int node,int level){
if(adjlist[node].empty()){
levelleaves[level]++;
return;
}
vector<int>:: iterator itea = adjlist[node].begin();
for(;itea!=adjlist[node].end();itea++){
dfs(*itea,level+);
}
} int main()
{
int n,m;
cin>>n>>m;
int leaves = n-m;
for(int i=;i<m;i++){
int id1,k, id2;
cin>>id1>>k;
for(int j=;j<k;j++){
cin>>id2;
adjlist[id1].push_back(id2);
}
}
dfs(,);
int a=levelleaves[];
cout<<levelleaves[];
for(int i=;a<leaves;i++){
cout<<" "<<levelleaves[i];
a=a+levelleaves[i];
}
return ;
}


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. ES5 Objece.creat实现继承

    Object.create() Object.create(proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不是一个子函数, ...

  2. Centos 7 mysql Buffered warning: Changed limits: max_connections: 214 解决方法

    Everytime I restart MySQL I have this warning: [Warning] Buffered warning: Changed limits: max_conne ...

  3. hdu 3839 Ancient Messages (dfs )

    题目大意:给出一幅画,找出里面的象形文字. 要你翻译这幅画,把象形文字按字典序输出. 思路:象形文字有一些特点,分别有0个圈.1个圈.2个圈...5个圈.然后dfs或者bfs,就像油井问题一样,找出在 ...

  4. 在eclipse上开发nodejs

    首先到官网下载nodejs.地址:https://nodejs.org/en,可根据自己的操作系统选择下载. 安装好后.进入命令行输入node ,然后输入console.log("hello ...

  5. JavaScript,复习总结

    ECMA(European Computer Manufacturers Association)欧洲计算机制造商协会.其制定很多标准:C#语言规范:C++/CLI语言规范:Eiffel语言:CD-R ...

  6. ios 使用AFN上传图片到服务器

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSe ...

  7. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    参考: http://www.cnblogs.com/sunxucool/archive/2013/06/07/3124380.html   ---------------------------&g ...

  8. 003_关于IntellJ IDE 2016 1. 4的使用

    IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本工具( ...

  9. 我的git与github学习历程

    因为想要知道如何把代码放到github上,所以就百度了一下,然后找到一个<如何从github上面拷贝源码>的文章,就先进行练习了下   1.首先到git官网下载git版本控制工具的安装包, ...

  10. 2016 icpc-ec-final

    一不小心惨变旅游队,不过上海的风景不错 顺带找其他队交流一下集训经验...或许可以成为选拔和集训16级的依据 A.直接模3就可以了,2^(3*n)%7=1 L.每场比赛3种情况,穷举就可以了 D.刚开 ...