# 1004. Counting Leaves (30)

135人阅读 评论(0)

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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：14382次
• 积分：1101
• 等级：
• 排名：千里之外
• 原创：102篇
• 转载：0篇
• 译文：0篇
• 评论：2条
阅读排行
评论排行
最新评论