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 Specification:

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.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

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

程序&算法

两个问题:
1.用什么数据结构?
为了简单,我用的是结构数组。
2.每个结点的level怎么确定?
T[i].level=T[T[i].parent].level+1;
要注意,当前结点的level没有办法在输入的时候就确认,虽然你已经知道parent,但是parent的level很有可能还没有确认,所以只能先储存parent的位置。

接下来我用的是递归来确认level,从root开始一个个往下遍历。因为我用的是数组,时间复杂度是n^2。用链表数组来存储,这部分复杂度会好很多。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MaxSize 101

struct TNode{
    int level;
    int IsNoChild;
    int parent;
};
void InputData(int M);
void CalculateLevel(int root,int N);
struct TNode T[MaxSize];
int Lever[MaxSize];

int main()
{
    int N,M,cnt=0;
    scanf("%d %d",&N,&M);
    /*初始化T[MaxSize]*/
    for(int i=1;i<=N;i++)
    {
        T[i].level = 0;
        T[i].IsNoChild = 1;
        T[i].parent = 0;
    }
    InputData(M);

// {
// //test1: 验证结构数组是否Input Right
// for(int i=1;i<=N;i++)
// printf("T[%d].level = %d,T[%d].IsNoChild = %d,T[%d].parent = %d\n",i,T[i].level,i,T[i].IsNoChild,i,T[i].parent);
// }

    for(int i=0;i<N;i++)
    {
        Lever[i]=0;
    }
    CalculateLevel(1,N);
    /*数组Lever[MaxSize]存储每个level叶子结点的个数,并计算最大level*/
    for(int i=1;i<=N;i++)
    {
        if(T[i].IsNoChild) Lever[T[i].level]++;
        if(T[i].level>cnt) cnt = T[i].level;
    }

    for(int i=0;i<cnt;i++)
    {
        printf("%d ",Lever[i]);
    }
    printf("%d",Lever[cnt]);
}


void InputData(int M)
{
    int par,child,num;
    for(int i=0;i<M;i++)
    {
        scanf("%d %d",&par,&num);
        T[par].IsNoChild = 0;
        for(int j=0;j<num;j++)
        {
            scanf("%d",&child);
            T[child].parent = par;
        }
    }
}
/*递归确认level*/
void CalculateLevel(int root,int N)
{
    for(int i=0;i<=N;i++)
    {
        if(T[i].parent==root)
        {
             T[i].level = T[root].level+1;
             CalculateLevel(i,N);
        }
    }
}
全部评论

相关推荐

11-28 17:48
中山大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务