A1004. Counting Leaves

时间:2022-12-21 19:19:43

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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef struct NODE{
vector<int>child;
int layer;
}node;
node tree[];
int N, M, cnt[] = {,}, depth = -;
void levelOrder(int root){
queue<int> Q;
tree[root].layer = ;
Q.push(root);
while(Q.empty() == false){
int temp = Q.front();
if(tree[temp].layer > depth)
depth = tree[temp].layer;
Q.pop();
if(tree[temp].child.size() == ){
cnt[tree[temp].layer]++;
}
int len = tree[temp].child.size();
for(int i = ; i < len; i++){
tree[tree[temp].child[i]].layer = tree[temp].layer + ;
Q.push(tree[temp].child[i]);
}
}
}
int main(){
int tempc, tempd, tempe;
scanf("%d%d", &N, &M);
for(int i = ; i < M; i++){
scanf("%d%d", &tempc, &tempd);
for(int j = ; j < tempd; j++){
scanf("%d",&tempe);
tree[tempc].child.push_back(tempe);
}
}
levelOrder();
for(int i = ; i <= depth; i++){
if(i != depth)
printf("%d ", cnt[i]);
else printf("%d", cnt[i]);
}
cin >> N;
return ;
}

总结:

1、题意:题目要求计算从根开始每一层的没有孩子的家庭成员。其实从题目上就知道,就是计算每一层的叶节点个数。

2、使用一个hash数组,以层数为下标索引。使用层序遍历来计算出每一个节点的层数。每次访问节点时,如果该节点没有子树,则将其所在layer的hash数组加一。

A1004. Counting Leaves的更多相关文章

  1. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

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

  2. PAT甲级——A1004 Counting Leaves

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

  3. PAT&lowbar;A1004&num;Counting Leaves

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

  4. 1004&period; Counting Leaves &lpar;30&rpar;

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

  5. PAT 解题报告 1004&period; Counting Leaves &lpar;30&rpar;

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

  6. PAT1004&colon;Counting Leaves

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

  7. PTA &lpar;Advanced Level&rpar; 1004 Counting Leaves

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

  8. PAT-1004 Counting Leaves

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

  9. PAT甲1004 Counting Leaves【dfs】

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

随机推荐

  1. 如何把Excel中的某列数值如何转换成文本格式&comma;且兼容性最好&quest;

    假设要把下方A列数值变成文本格式.有多种方法,其中下方的方法兼容性最好: 第一步:选择 A 列,然后,运行菜单 数据/分列... 命令,如下图: 第二步:默认 下一步: 第三步:默认 下一步: 第四步 ...

  2. 使用github托管代码心

    这次使用github托管代码并没有下载客户端git for windows,而是使用eclipse里面自带的git上传了hello world这个项目,步骤如下: 1.首先创建项目:file-> ...

  3. python 网络编程-TCP&sol;UDP

    摘抄自:廖雪峰的官方网站:http://www.liaoxuefeng.com/ TCP客户端和服务器端代码: #coding=utf-8 #客户端程序TCP 连接 import socket s=s ...

  4. 责任链模式(Chain of Responsibility Pattern)

    责任链模式:可以为某个请求创建一个对象链.每个对象依序检查此请求,并对其处理,或者把它传给链中的下一个对象. 责任链上的对象负责处理请求,客户只需要将请求发送到责任链上即可,无需关心处理的细节和请求的 ...

  5. 单页应用引擎的写法artTemplate

    使用到了ba-haschange.js <script src="../style/js/plugin/template-native-debug.js"></s ...

  6. xUtils3源码分析(一):view的绑定

    概述 xUtils3是国人开发的一款功能丰富的Android快速开发框架,值得研究下.zip包下载:[ZIP]xutils主要分以下几个模块 视图绑定模块 网络请求模块 数据库模块 图片加载模块 我们 ...

  7. Android实现获取本机中所有图片

    本示例演示如何在Android中使用加载器(Loader)来实现获取本机中的所有图片,并进行查看图片的效果. 在这个示例中,我使用android-support-v4.jar中的加载器(Loader) ...

  8. LFM 隐语义模型

    隐语义模型: 物品       表示为长度为k的向量q(每个分量都表示  物品具有某个特征的程度) 用户兴趣 表示为长度为k的向量p(每个分量都表示  用户对某个特征的喜好程度) 用户u对物品i的兴趣 ...

  9. Bootstrap兼容IE8

    使用BootStrap3.x写的公司一个响应式项目在IE下面错误百出,经过一番折腾.全部解决了IE8下的兼容问题. 这里汇总一下,希望对大家有所帮助. 1. Bootstrap UI整体在IE8下变窄 ...

  10. Ubuntu:命令行下浏览网页

    前述 兴起,试一下不用图形化界面浏览 安装w3m 直接进入root账号 apt-get install w3m 检验是否成功 w3m www.baidu.com 就这样成功的进入baidu了,纯文本模 ...