1004 Counting Leaves (30)(30 point(s))
problem
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
tip
一颗树,找到每层的叶子数量。
anwser
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define Max 101
#define mp(x, y) std::make_pair(x, y)
int N, M, level[Max], maxLevel = 0;
std::vector<int > kids[Max];
//typedef std::pair<int ,int> node
int main(){
// freopen("test.txt", "r", stdin);
memset(level, 0, sizeof(level));
std::cin>>N>>M;
int fa, kidNum, kid;
for(int i = 0; i < M; i++){
std::cin>>fa>>kidNum;
for(int j = 0; j < kidNum; j++){
std::cin>>kid;
kids[fa].push_back(kid);
// std::cout<<kid<<" ";
}
// std::cout<<std::endl;
}
// for(int i = 1; i <= N; i++) {
// if(i == 1)std::cout<< kids[i].size();
// else std::cout<<" "<<kids[i].size();
// }
std::queue<std::pair<int, int> > que;
que.push(mp(1,0));
while(!que.empty()){
std::pair<int, int> father = que.front(); que.pop();
if(kids[father.first].size() == 0) {
// std::cout<<father.first<<std::endl;
level[father.second] ++;
maxLevel = father.second;
continue;
}
for(int i = 0; i < kids[father.first].size(); i++){
que.push(mp(kids[father.first][i], father.second+1));
}
}
for(int i = 0; i <= maxLevel; i++)
{
if (i == 0) std::cout<<level[i];
else std::cout<<" "<<level[i];
}
return 0;
}
/*
4 2
01 1 02
02 2 03 04
*/
experience
英语单词:
- hierarchy 层级关系
1004 Counting Leaves (30)(30 point(s))的更多相关文章
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 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 ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- A1095 Cars on Campus (30)(30 分)
A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
随机推荐
- AAC编码
1. 前言 如果说目前H.264是视频CODEC的实际霸主,那么AAC就是音频CODEC的女王.主流的音视频格式都是H.264搭配AAC,无论是非实时的媒体文件还是实时的媒体流. 2. AAC历史 A ...
- 在mac环境下用QT使用OpenGL,glut,glfw
只需要在新建工程中.pro文件中添加: #opengl glut LIBS+= -framework opengl -framework glut 就可以使用glut了. 继续添加: ##glfw L ...
- Django 基础命令
- Go语言的各种Print函数
Go语言的各种Print函数 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) func Pr ...
- 结构体变量的sizeof计算
结构体字节对齐准则: 1. 结构体变量的首地址能够被其最宽基本类型成员的大小所整除: 2. 结构体每个成员相对于结构体首地址的偏移量都是当前成员大小的整数倍,如有需要编译器会在成员之间加上填充字节: ...
- Dream------Hadoop--Hadoop HA QJM (Quorum Journal Manager)
In a typical HA cluster, two separate machines are configured as NameNodes. At any point in time, ex ...
- collision
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAd0AAACYCAIAAAAuvaRSAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu
- .net基础初学Android
第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正则表达式. 3.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...
- 大数据系列之分布式计算批处理引擎MapReduce实践
关于MR的工作原理不做过多叙述,本文将对MapReduce的实例WordCount(单词计数程序)做实践,从而理解MapReduce的工作机制. WordCount: 1.应用场景,在大量文件中存储了 ...
- unity 2d 游戏优化之路 遇坑记录
情景说明: unity 出的Android包,在目前一些主流机型跑都没有问题,但是在 小米3 这种比较老的机器上跑,报如下错误 GLSL compilation failed, no infolog ...