1004. Counting Leaves (30)

来源:互联网 发布:博雅软件上市进展 编辑:程序博客网 时间:2024/05/13 09:57

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

题目分析:主要考察DFS、BFS、层序遍历,该题的难点就是怎么分层,这一点在代码的注释中会说明。

#include <stdio.h>#include <stdlib.h>int e[100][100] = {{0}}; //存储图//结点下标从1开始,这里比较巧妙的利用第0列存储当前结点的儿子数//可以作为判断该结点是否是叶节点的依据int strToNum(char a[]) //将字符串转为整数{    return (a[0] - '0') * 10 + a[1] - '0';}typedef struct QNode *queue; //创建队列struct QNode{    int vertex[100];    int pre, last;};int isEmpty(queue q){    if(q->pre == q->last)        return 1;    else        return 0;}void addQ(queue q, int v){    q->vertex[++q->last] = v;}int deleteQ(queue q){    return q->vertex[++q->pre];}queue createQ(){    queue q = (queue)malloc(sizeof(struct QNode));    q->last = q->pre = -1;    return q;}void BFS(int s, int n) //利用BFS得出结果{    int visited[100] = {0}; //表示一个结点是否被访问过    int v, w, count = 0, last = s, tail, f = 1;    //count用来记录每一层的叶节点数,last表示第一层的最后一个进入队列的结点    //tail表示下一层的最后一个进入队列的结点。f用来控制输出格式的    queue q = createQ();    addQ(q, s);    visited[s] = 1;    while(!isEmpty(q))    {        v = deleteQ(q);        visited[v] = 1;        for(w = 1; w <= n; w++)        {            if(e[v][0] == 0)            {                count++;                break;            }            if(!visited[w] && e[v][w] == 1)            {                addQ(q, w);                tail = w;                visited[w] = 1;            }        }        if(v == last) //代表每一层的结束,于是就输出这一层的叶节点数        {            if(f)            {                printf("%d", count);                f = 0;            }            else                printf(" %d", count);            count = 0;            last = tail;        }    }}int main(){    int n, m, i, j;    scanf("%d %d", &n, &m);    for(i = 0; i < m; i++)    {        char parent[3], child[3];        int t, k;        scanf("%s %d", parent, &k);        t = strToNum(parent);        e[t][0] = k; //结点下标从1开始,这里比较巧妙的利用第0列存储当前结点的儿子数        for(j = 0; j < k; j++)        {            scanf("%s", child);            e[t][strToNum(child)] = 1;        }    }    BFS(1, n);    return 0;}
1 0