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. Windows下底层数据包发送实战

    1.简介 所谓“底层数据包”指的是在“运行”于数据链路层的数据包,简单的说就是“以太网帧”,而我们常用的Socket只能发送“运行”在传输层的TCP.UDP等包,这些传输层数据包已经能满足绝大部分需求 ...

  2. django 同步数据库

    http://www.jianshu.com/p/dbc4193b4f95 博主教程讲解比较详细,可做参考使用.

  3. Monkey的ADB命令简单使用示例和解析

    进行简单的压力测试: 1. adb shell monkey –p 包名 –v-v 3000 >E:\bugLog.txt -v -v 标识打印的日志的详细级别为2级,更高级有3级,也可以用1级 ...

  4. ODBC简介

    加载驱动 1 oracle Class.forName("oracle.JDBC.driver.OracleDriver") 2 DB2 Class.forName("c ...

  5. STM32的USART中断死循环,形成死机。

    作者:观海  QQ:531622 直接说重点:我用的是 STM32F103 芯片 USART2_IRQHandler 总是中断,程序死循环. 1.出现问题: 原程序的中断处理程序是: void USA ...

  6. SQL 语句格式

    SELECT `menuid`, SUM(`num`)AS total, `storeid`, DATE_FORMAT(`dateline`,'%Y-%m-%d') days FROM loss WH ...

  7. 65279 !!!BOM

    java.lang.NumberFormatException: For input string: "1".莫名其妙的String的第一个字符,是个空格样的东西,但绝对不是空格, ...

  8. MYSQL中存储过程的创建,调用及语法

    MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库 ...

  9. SpringMVC框架下的拦截器

    在eclipse的javaEE环境下:导包.... web.xml文件中的配置: <?xml version="1.0" encoding="UTF-8" ...

  10. Bootstrap学习(2)--表单

    Bootstrap里的role属性,增强标签的语义化,提高识别力,  如:<form role="form"> input.select.textarea等元素,在Bo ...