PAT1004:Counting Leaves
1004. Counting Leaves (30)
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 思路 简单题,主要考察树的层次遍历。
用一个邻接表来构建树然后bfs就行了,bfs时插入一个label区分树的层级,然后统计叶子节点就行。
代码
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
vector<vector<int>> tree(100);
vector<int> levels;
vector<bool> isroot(100,true);
const int label = -2; void bfs(const int& root)
{
queue<int> q;
q.push(root);
q.push(label);
int countleaves = 0;
while(!q.empty())
{
int tmp = q.front();
q.pop();
if(tmp == label)
{
levels.push_back(countleaves);
countleaves = 0;
if(!q.empty())
q.push(label);
continue;
}
if(tree[tmp].empty())
countleaves++;
else
{
for(int i = 0;i < tree[tmp].size();i++)
{
q.push(tree[tmp][i]);
}
}
}
} int main()
{
int N,M;
while(cin >> N >> M)
{
for(int i = 1;i <= M;i++)
{
int cur,K;
cin >> cur >> K;
for(int j = 0;j < K;j++)
{
int tmp;
cin >> tmp;
isroot[tmp] = false;
tree[cur].push_back(tmp);
}
} int root = -1;
for(int i = 1;i <= N;i++)
{
if(isroot[i])
{
root = i;
break;
}
}
bfs(root); cout << levels[0];
for(int i = 1;i < levels.size();i++)
cout << " " << levels[i];
cout << endl;
}
}
PAT1004:Counting Leaves的更多相关文章
- PAT-1004 Counting Leaves
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- pat1004. Counting Leaves (30)
1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...
- 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 ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Counting Leaves[一般]
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 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 ...
- PAT_A1004#Counting Leaves
Source: PAT A1004 Counting Leaves (30 分) Description: A family hierarchy is usually presented by a p ...
随机推荐
- JavaScript进阶(十一)JsJava2.0版本
JavaScript进阶(十一)JsJava2.0版本 2007年9月11日,JsJava团队发布了JsJava2.0版本,该版本不仅增加了许多新的类库,而且参照J2SE1.4,大量使用了类的继承和实 ...
- 实用Android 屏幕适配方案分享
转载地址:http://blog.csdn.net/gao_chun/article/details/45645051 真正可用,并且简单易行,可以在多个屏幕大小和屏幕密度上有良好表现的Android ...
- 【Qt编程】Qt版扫雷
学习要学会举一反三.在以前的<用matlab扫扫雷>一文中,我用matlab简单的编写了一个扫雷小程序.当然,与Windows自带的扫雷程序自然是不敢相提并论.今天我就用c++来写个扫雷程 ...
- 《java入门第一季》之面向对象(代码块一网打尽)
上一篇里面对代码块做出介绍,这里给出一个面试题,加深印象. 如有毁三观的地方,请见谅.拒绝黄赌毒 写程序的执行结果. class Student { static { System.out.print ...
- TCP的核心系列 — SACK和DSACK的实现(二)
和18版本相比,37版本的SACK和DSACK的实现做了很多改进,最明显的就是需要遍历的次数少了, 减少了CPU的消耗.37版的性能提升了,代码有大幅度的改动,逻辑也更加复杂了. 本文主要内容:37版 ...
- iOS雷达图 iOS RadarChart实现
实现效果 刚拿到设计稿的时候大概看了一眼,当时心里想着放张背景图,然后计算下相应点的坐标,在最上面画一层就OK了,其实一开始实现的时候也确实是这么做的,然后我就日了狗了,发现设计稿上多层五边形的间隔不 ...
- AES涉及的有限域乘法及字节填充方法
非常值得参考的是官方文档,它详细介绍了AES及其实验过程.博文AES加密算法的C++实现就是基于该文档的介绍及实现,是难得的一篇好文,故在本文最后会附上该文,以作备份. 还有很值得推荐的就是AES的 ...
- C++之IO操作
可参考自: C++之标准设备IO操作流 C++之预定义类型IO格式控制 C++之文件IO操作流
- Gradle 1.12用户指南翻译——第三十九章. IDEA 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- 原生的zfs在rhel6上的安装
原生的zfs在rhel6上的安装 ZFS(Zettabyte File System)作为一个全新的文件系统,全面抛弃传统File System + Volume Manager + Storage( ...