【PAT甲级】1004 Counting Leaves (30 分)

1004 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 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

思路:

首先分析一下样例输出,样例有两个节点,有一个非叶子节点01,非叶子节点有1个孩子节点02,这棵树有两层,一层是01,一层是02,第一层叶子节点个数为0,第二层叶子节点个数为1,因此输出0 1

本题是典型的广度优先搜索算法,从根节点开始,层层遍历每一层的节点,储存其信息,最后输出即可。

代码:

首先重要的要定义结构体node:
其中level代表该节点的层数,num表示该节点的孩子个数,child是一个可变数组,用于存储其孩子的节点序号

struct node
{
    
    
    int level;
    int num;//numbers of this node's children
    int *child;//the array store its child
};

接下来是全局变量

#define MAXN 100

int N,M;//the number of nodes in a tree,the number of non-leaf nodes.
bool falg=true;
node family[MAXN];
int queue[MAXN],pt=1;

其中falg用于判断是否输入的N为0【保证没有错误情况发生】,queue队列是用于广度优先搜索的,pt是这个队列的长度全局指针

初始化函数:

void init()
{
    
    
    scanf("%d %d",&N,&M);
    if(N==0)
    {
    
    
        falg=false;
        return;
    }
    for(int i=1;i<=N;i++)//将所有节点的level暂设为1,所有节点没有孩子
    {
    
    
        family[i].level=0;
        family[i].num=0;
    }
    for(int i=0;i<M;i++)
    {
    
    
        int ID,K,temp;scanf("%d %d",&ID,&K);
        //记录节点ID的孩子的个数K,并new一个数组,记录孩子的编号
        family[ID].num=K;
        family[ID].child=new int[K];//new its child array
        for(int i=0;i<K;i++)
        {
    
    
            scanf("%d",&temp);
            family[ID].child[i]=temp;
        }
    }
    return;
}

BFS函数:

void BFS()
{
    
    
	//首先将根节点放入队列
    queue[pt++]=1;
    for(int i=1;i<pt;i++)//由于每次放入新的节点pt都会增加,这样就实现了队列的方法,i表示队列的头,pt是尾部
    {
    
    
        for(int j=0;j<family[queue[i]].num;j++)//对于i节点的所有孩子节点
        {
    
    
            int child=family[queue[i]].child[j];//记录i节点的第j个孩子节点序号
            queue[pt++]=child;//将其放入队列
            family[child].level=family[i].level+1;//同时这个孩子的层数是i节点层数+1
        }
    }
    return;
}

最后计算每层有多少叶子节点

void output()
{
    
    
    if(!falg)
        return;
    int max=0;
    for(int i=1;i<=N;i++)//首先要计算最大层数
    {
    
    
        max=max>family[i].level?max:family[i].level;
    }
    //根据最大层的个数构建长度为max+1的数组,用于储存每一层的叶子节点的个数
    int *level=new int[max+1];
    for(int i=1;i<=N;i++)
    {
    
    
    	//遍历所有节点,如果某一节点没有孩子,证明其是叶子节点,将对应层的叶子节点个数+1
        if(family[i].num==0)
        {
    
    
            level[family[i].level]++;
        }
    }
    //输出所有层的叶子节点个数
    for(int i=0;i<max+1;i++)
    {
    
    
        if(i==0)
            printf("%d",level[i]);
        else
            printf(" %d",level[i]);
    }
    printf("\n");
    return;
}

主函数

#include<stdio.h>

int main()
{
    
    
    init();
    BFS();
    output();
    return 0;
}

git仓库:Counting Leaves

猜你喜欢

转载自blog.csdn.net/qq_35779286/article/details/95116876