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_A1004#Counting Leaves

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

  4. 1004. Counting Leaves (30)

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

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

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

  6. PAT1004:Counting Leaves

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

  7. PTA (Advanced Level) 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. hive 查询注意问题

    1)对于hive内置的列,不是自己建的,在查询的时候需要添加反引号` 比如:`_mt_message`,别在这里犯错误, (2)南京的_mt_message是json的格式,所以可以直接使用:get_ ...

  2. python RSA 加密与签名

    PyCrypto装起来就简单多了,我是直接 sudo easy_install pycrypto 直接搞定的 先生成rsa的公私钥:打开控制台,输入 openssl 再输入 genrsa -out p ...

  3. linux 挂载windows下目录,其它linux机器nfs的目录,自己dd的文件

    如有转载,不胜荣幸.http://www.cnblogs.com/aaron-agu/ 挂载window下共享的目录 //192.168.0.11/share /mnt 挂载其它linux机器下目录 ...

  4. Java多线程之线程状态转换图

    说明:线程共包括以下5种状态.1. 新建状态(New)         : 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread().2. 就绪状态(Runn ...

  5. Coalesce (MS SQL Server)——取指定内容(列)中第一个不为空的值

    oalesce 获得参数中第一个不为空的表达式. 语法:        COALESCE ( expression [ ,...n ] ) 例子:CREATE TABLE wages          ...

  6. jpa 比较复杂的查询和用in关键字

    in关键字使用代码

  7. 自定义 ASP.NET Identity Data Model with EF

    One of the first issues you will likely encounter when getting started with ASP.NET Identity centers ...

  8. zabbix自动注册

    实现方法是: 第一: 选择动作-->事件源-->自动注册-->创建动作 第二: 动作-->触发条件-->主机元数据-->contains-->Linux 第四 ...

  9. linu系统文件授权命令

  10. P1567 气温统计

    P1567 题目描述 炎热的夏日,KC 非常的不爽.他宁可忍受北极的寒冷,也不愿忍受厦门的夏天.最近,他开始研究天气的变化.他希望用研究的结果预测未来的天气. 经历千辛万苦,他收集了连续 N(1≤N≤ ...