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

题目描述:

统计一颗树每一层的leaf数量。

算法分析:

思路1:BFS

本质就是lever order traversal, 可以用bfs遍历,然后每一层统计叶子数。

思路2:DFS

可以使用邻接矩阵的方式定义树结构。然后使用 dfs 遍历树的节点,并记录每层的叶子节点数量。 可以看到,时间空间的 trade-off 不仅仅是性能上的提升,也会影响带代码实现的复杂程度。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; #define MX 101 int mp[MX][MX];
queue<int> que;
int n,m; int bfs(int s) {
int flag = ;
for (int i=; i<=n; i++) {
if (mp[s][i] == ) {
que.push(i);
flag = ;
}
}
return flag;
} void actbfs() {
que.push();
que.push();
int cnt = ;
while (!que.empty()) {
int s = que.front();
que.pop();
if (s == ) {
if (que.empty()) {
printf("%d", cnt);
break;
}
else {
que.push();
printf("%d ", cnt);
cnt = ;
}
}
else {
int flag = bfs(s);
cnt += flag;
}
}
} int main()
{
scanf("%d%d", &n,&m);
memset(mp, , sizeof(mp));
for (int i=; i<m; i++) {
int id,k;
scanf("%d%d", &id,&k);
for (int j=; j<k; j++) {
int chi;
scanf("%d", &chi);
mp[id][chi] = ;
}
} actbfs(); return ;
}
												

PAT 解题报告 1004. Counting Leaves (30)的更多相关文章

  1. PAT 解题报告 1049. Counting Ones (30)

    1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...

  2. PAT (Advanced Level) 1004. Counting Leaves (30)

    简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  3. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

    题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...

  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. 1004. Counting Leaves (30)

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

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

  7. 1004 Counting Leaves (30分) DFS

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

  8. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  9. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

随机推荐

  1. 【翻译】Kinect v2程序设计(C++) Body 篇

    Kinect SDK v2预览版的主要功能的使用介绍,基本上完成了.这次,是关于取得Body(人体姿势)方法的说明.   上一节,是使用Kinect SDK v2预览版从Kinect v2预览版取得B ...

  2. 总结一下这几天学习django的心得

    总结一下这几天学习django的心得 http://www.tuicool.com/articles/jMVB3e 时间 2014-01-12 11:40:11  CSDN博客 原文  http:// ...

  3. STM32全球唯一ID读取方法

    产品唯一的身份标识非常适合:● 用来作为序列号(例如USB字符序列号或者其他的终端应用)● 用来作为密码,在编写闪存时,将此唯一标识与软件加解密算法结合使用,提高代码在闪存存储器内的安全性.● 用来激 ...

  4. HTML: css 修飾文本和字體

    因爲這個我認爲不用記,所以關於css 修飾文本&字體的屬性只需要打開css手冊,找到(屬性 > 文本) & (屬性 > 字體)翻看即可. 關於字體屬性: Propertie ...

  5. 验证码 mewebstudio/captcha

    composer require mews/captcha https://github.com/mewebstudio/captcha

  6. Virtual Memory PAGE TABLE STRUCTURE

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The basic mechanism f ...

  7. Delphi XE5 与其他版本共存

    来源于http://www.board4allcz.eu 作者是gateway 如果你想使用Delphi诸如XE4.XE3.XE2.XE之类的版本跟Delphi XE5共存的话,在cglm.ini中简 ...

  8. 超级管理员登录后如果连续XX分钟没有操作再次操作需要重新登录

    首先在设置session页面时 session_start(); session("name",$adminname); //加入session时间 time() session( ...

  9. JavaScipt选取文档元素的方法

    摘自JavaScript权威指南(jQuery根据样式选择器查找元素的终极方式是 先用getElementsByTagName(*)获取所有DOM元素,然后根据样式选择器对所有DOM元素进行筛选) 选 ...

  10. css3常用动画效果集合01

    /*由右到左进场*/ .FromRightToLeft{ -webkit-animation:FromRightToLeft 500s .2s ease both; } @-webkit-keyfra ...