1004. Counting Leaves (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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

思路

简单题,主要考察树的层次遍历。
用一个邻接表来构建树然后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的更多相关文章

  1. PAT-1004 Counting Leaves

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

  2. pat1004. Counting Leaves (30)

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

  3. 1004. Counting Leaves (30)

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

  4. PAT 解题报告 1004. Counting Leaves (30)

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

  5. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  6. PAT甲1004 Counting Leaves【dfs】

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

  7. PAT Counting Leaves[一般]

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

  8. 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 ...

  9. PAT_A1004#Counting Leaves

    Source: PAT A1004 Counting Leaves (30 分) Description: A family hierarchy is usually presented by a p ...

随机推荐

  1. 【matlab编程】matlab随机数函数

    Matlab内部函数 a. 基本随机数 Matlab中有两个最基本生成随机数的函数. 1.rand() 生成(0,1)区间上均匀分布的随机变量.基本语法: rand([M,N,P ...]) 生成排列 ...

  2. 典型分布式系统分析: GFS

    本文是典型分布式系统分析系列的第二篇,关注的是GFS,一个分布式文件存储系统.在前面介绍MapReduce的时候也提到,MapReduce的原始输入文件和最终输出都是存放在GFS上的,GFS保证了数据 ...

  3. 【61】git项目实战的步骤总结

    1.新建分支的步骤 git pull git checkout -b 分支号(task的后面的代号) 2.提交代码到远程仓库的步骤 git add . git commit -m "分支号+ ...

  4. 【面试笔试算法】Program 5 : 推箱子 (网易游戏笔试题)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 推箱子是一款经典游戏.如图所示,灰色格子代表不能通过区域,蓝色方格是箱子,黑色圆形代表玩家,含有圆点的格子代表目标点. 规 ...

  5. MTK Android software Tools工具的说明

    MTK发布的Android software Tools工具包,里面包含了很多的MTK工具,如下是简要说明及学习文档 MTK Android software Tools工具的说明如下: 工具 说明 ...

  6. ITU-R BT.1788建议书 对多媒体应用中视频质量的主观评估方法

    ITU-R BT.1788建议书 对多媒体应用中视频质量的主观评估方法 (ITU‑R 102/6号研究课题) (2007年) 范围 数字广播系统允许提供多媒体和数据广播应用,包括视频.音频.静态图像. ...

  7. LIRe 源代码分析 5:提取特征向量[以颜色布局为例]

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  8. Android中使用SVG矢量图(一)

    SVG矢量图介绍 首先要解释下什么是矢量图像,什么是位图图像? 1.矢量图像:SVG (Scalable Vector Graphics, 可伸缩矢量图形) 是W3C 推出的一种开放标准的文本式矢量图 ...

  9. Socket层实现系列 — bind()的实现(一)

    bind()函数的使用方法很简单,但是它是怎么实现的呢? 笔者从应用层出发,沿着网络协议栈,分析了bind()的系统调用.Socket层实现,以及它的TCP层实现. 本文主要内容:bind()的系统调 ...

  10. C# DataTable,DataSet,IList,IEnumerable 互转扩展属性

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...