# 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

------------------------------华丽的分割线----------------------------

#include<cstdio>
#include<queue>
using namespace std;

#define Maxn 101

int graph[Maxn][Maxn];
int NumOfNoLeaf[Maxn];
int Level=1;
bool HaveNoChild=true;
queue<int> findnoleaf;

int main(void)
{
int N,M,K;
int i,j;
scanf("%d %d",&N,&M);
for(i=0;i<M;++i)
{
int parent,child;
scanf("%2d %d",&parent,&K);
for(j=0;j<K;++j)
{
scanf("%2d",&child);
graph[parent][child] = 1;
}
}

int startnode=1;
graph[startnode][startnode] = 1;
int thisnode=0;

findnoleaf.push(startnode);
while(!findnoleaf.empty()){
thisnode = findnoleaf.front();
findnoleaf.pop();
for(i=1;i<=N;++i)
{
if(graph[thisnode][i] != 0 && thisnode != i)
{
graph[i][i] = graph[thisnode][thisnode]+1;
findnoleaf.push(i);
if(graph[i][i] > Level)
Level = graph[i][i];
HaveNoChild = false;
}
}
if(HaveNoChild)
{
++NumOfNoLeaf[graph[thisnode][thisnode]];
}
else
{
HaveNoChild = true;
}
}

for(i=1;i<=Level;++i)
{
printf("%d",NumOfNoLeaf[i]);
if(i!=Level)
printf(" ");
}
return 0;
}

• 本文已收录于以下专栏：

## pat1004Counting Leaves (30)

• LostStephon
• 2015年10月02日 14:32
• 511

## PAT甲级 1004.Counting Leaves (30) 题目翻译与答案

1004.计算叶子个数 一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。 输入 每个输入文件包含一个测试实例。每个实例开始的一行包含N和M，N指树中的结点个数（0...
• qq278672818
• 2017年02月07日 20:29
• 600

## 1004. Counting Leaves (30)——PAT (Advanced Level) Practise

• xianyun2009
• 2014年09月23日 17:15
• 762

## PAT 1004. Counting Leaves (30)（Java一个测试点未通过 and C++）

1004. Counting Leaves (30) Java代码：diwu
• u010388829
• 2014年09月10日 09:14
• 817

## 浙大PAT 1004 Counting Leaves （简单深搜）

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard...
• IAccepted
• 2014年03月15日 16:32
• 5009

## 1004. Counting Leaves (30)

1004. Counting Leaves (30) #include #include #include using namespace std; class A { public:...
• gk2007310201668
• 2013年11月12日 16:39
• 684

## PAT 1004. Counting Leaves (30)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard...
• u010069101
• 2014年02月25日 17:12
• 475

## PAT_A 1004. Counting Leaves (30)

1004. Counting Leaves (30) 树
• scylhy
• 2016年11月20日 15:03
• 222

## 【C++】1004. Counting Leaves (30)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者...
• artemisrj
• 2014年03月18日 20:20
• 442

## 1004. Counting Leaves (30)-DFS

• wanmeiwushang
• 2016年08月03日 17:42
• 237

举报原因： 您举报文章：1004. Counting Leaves (30) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)