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. TVP5150摄像头

    工作中看同事摄像头配置的时候有2种格式PAL.NTSC.如果摄像头的格式配置不对的话会出现重影.黑白没颜色.闪屏等等. TVP5150  PAL.NTSC配置.http://bbs.csdn.net/ ...

  2. Jsp入门学习笔记

    #Jsp入门 一.JSP基础语法 1.JSP指令: page inlcude taglib 2.JSP注释: a.html注释: <!-- abcdefghijklmn --> b.jsp ...

  3. phpspec安装配置

    安装  composer require phpspec/phpspec -dev 运行  bin/phpspec 在laravel中  vendor/bin/phpspec 配置phpspec.ym ...

  4. dynamic-link library shared library of functions and resources

    https://msdn.microsoft.com/en-us/library/1ez7dh12.aspx A dynamic-link library (DLL) is an executable ...

  5. Windows 下 Nginx + PHP + Xdebug + PHPStorm 调试环境配置

    前期条件:安装好 Nginx.PHP.PHPStorm,使得可以正常访问 一.为 PHP 安装 Xdebug 到 Xdebug 的官网(http://xdebug.org/download.php)下 ...

  6. 【转】Unity3D开发之Http协议网络通信

    之前unity3d项目要做跟服务器通信的模块,然后服务器那边的协议是基于http的Jsonrpc通信方式一开始,用C#的本身类HttpWebRequest来提交请求,很快就在电脑上面成功了,代码也很简 ...

  7. Freemarker的第二次使用~list的元素是数组

    在上次初次使用Freemarker作为模版后,我再次使用它.这次的需求是: xml文档的某个节点的属性A和其一个子节点的某个属性B有关联,属性B的值需要随着属性A的值变化.而属性A的值有4个值,现在需 ...

  8. 更新Delphi中SVN客户端版本的方法

    Delphi从XE以后集成里SVN客户端, 安装完Delphi以后, 在bin\subversion下, 存放的就是SVN客户端文件, 可惜版本有点低(好像是1.7的) 如果想更新成高版本的客户端文件 ...

  9. Lambda中的一些方法的总结

    public List<UserInfoBaseModel> GetNameByIDList(List<int> UserID) { var UserList = LoadRe ...

  10. HTML5的新事件

    HTML 元素可拥有事件属性,这些属性在浏览器中触发行为,比如当用户单击一个 HTML 元素时启动一段 JavaScript. HTML 元素可拥有事件属性,这些属性在浏览器中触发行为,比如当用户单击 ...