A1004. Counting Leaves
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的更多相关文章
- 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 ...
- PAT甲级——A1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- PAT_A1004#Counting Leaves
Source: PAT A1004 Counting Leaves (30 分) Description: A family hierarchy is usually presented by a p ...
- 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 ...
- PAT1004:Counting Leaves
1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...
- 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
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
随机推荐
- DAY01、计算机组成及操作系统
一.编程与编程的目的: 1.什么是语言?什么是编程语言? 语言是一事物与另一事物之间沟通的介质 编程语言就是程序员与计算机之间沟通的介质 2.什么是编程? 程序员把自己想要让计算机做的事用编程语言表达 ...
- python数据结构与算法第四天【代码执行时间测试模块】
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ from timeit import Timer def foo(): ''' 使用append方式向列表添加 ...
- PHPWord插件详解
一下载PHPWorld并配置项目 1.PHPWord框架文件如下: 二使用word模板并使用PHPWord生成doc文件 例如:源代码如下: <?php require_once '../PHP ...
- linux 地址解析协议 arp
随便转载,保留出处:http://www.cnblogs.com/aaron-agu/ arp –na #查看 arp –s 123.253.68.209 00:19:56:6F:87:D4 #添加
- CSS 常见的8种选择器 和 文本溢出问题
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>c ...
- Hack You CTF 2014: NotEasyTask
exe文件,运行后闪退,peid查一下,发现是是c#写的 Reflector打开 找到Main: private static void Main(string[] args) { string ho ...
- 神烦之float
另外一篇文章 : css float 一 历史 Float的设计初衷仅仅是:文字环绕效果(向word中的文字环绕效果) 二 特性 1.包裹性:块级元素如果不设置float,它默认会撑满整个屏幕,而如果 ...
- Nginx 决策浏览器缓存是否有效
expires指令是告诉浏览器过期时间 syntax:expires [modified] time; eopch | max | off; default : off context :http,s ...
- workerman——配置小程序的wss协议
前言 服务器: 阿里云服务器 | 需要在安全组放开443端口和workerman需要的端口 环境: oneinstack | lnmp oneinstack添加虚拟主机的时候选择第三个即可 | 这个添 ...
- POJ 3020 -Antenna Placement-二分图匹配
题意:一个N*M的矩阵里有K个观测点,你必须放置天线覆盖所有观测点.每个雷达只能天线两个观测点,这两点必须相邻.计算最少天线数. 做法:将所有相邻的观测点连起来,建图.跑一遍匈牙利算法就计算出了最大的 ...