1004. Counting Leaves (30)

PAT题目:

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
题目看起来很长,实际上就是求一个树每一层的叶子数目。求每一层的叶子数目,最先想到的就是宽度优先搜索——BFS,于是就通过队列写了一个BFS搜索来计算每一层数目的多少:
#include <stdio.h>

#define MAXSIZE 200

typedef struct _non_leaf_node{
	int k;
	int child[100];
}non_leaf;

typedef struct _queue
{
	int node[MAXSIZE];
	int front,rear;
}queue;

int node[100];
non_leaf nl[100];
queue q;

int isFull(queue *q);
int isEmpty(queue *q);
int Enqueue(queue *q,int node);
int Dequeue(queue *q);

int main(int argc,char* argv[])
{
	int i,j,n,m,flag;
	int id,count,num;

	q.front = q.rear = 0;

	scanf("%d%d",&n,&m);

	for(i=0;i<n;i++)
		node[i] = 0;
	for(i=0;i<n;i++)
		nl[i].k = 0;

	for(i=0;i<m;i++)
	{
		scanf("%d",&id);
		scanf("%d",&nl[id].k);
		node[id] = 1;
		for(j=0;j<nl[id].k;j++)
			scanf("%d",&nl[id].child[j]);
	}

	if(n==1)
	{
		printf("0\n");
		return 0;
	}


	Enqueue(&q,1);
	Enqueue(&q,0);

	num = flag = 0;
	while(num <= m)
	{
		if( flag )
			break;
		if(num == m)
			flag = 1;
		id = Dequeue(&q);
		count = 0;
		while(id != 0){
			if(node[id] == 0)
				count += 1;
			else
				num += 1;
			for(j=0;j<nl[id].k;j++)
				Enqueue(&q,nl[id].child[j]);
			id = Dequeue(&q);
		}
		Enqueue(&q,0);
		printf("%d",count);
		if(!flag)
			printf(" ");
	}

	printf("\n");

	return 0;
}


int isFull(queue *q)
{
	return (q->rear+1)%MAXSIZE==q->front;
}

int isEmpty(queue *q)
{
	return q->rear==q->front;
}

int Enqueue(queue *q,int node)
{
	if( isFull(q) )
		return 0;
	else
	{
		q->node[q->rear++] = node;
		return 1;
	}
}

int Dequeue(queue *q)
{
	if( isEmpty(q) )
		return -1;
	else
	{
		q->front += 1;
		return q->node[q->front-1];
	}
}

写完之后,结果有一个case一直不通过,而且还有一个case因为队列的大小开小了导致段错误。最后找了好久都没找到最后那个case到底错那了,于是又想到来一次深度搜索应该也可以,就又写了个深度搜索,发现代码很短,每次深度递归的边界很好控制,很快就写好了,代码如下:

#include <stdio.h>

int a[100][100];
int b[100];
int level[100];
int maxlevel;

int dfs(int num,int l);

int main(int argc,char* argv[])
{
	int i,j,n,m,id,k;

	maxlevel = 0;

	scanf("%d%d",&n,&m);

	for(i=0;i<100;i++)
		b[i] = 0;

	for(i=0;i<m;i++)
	{
		scanf("%d%d",&id,&k);
		b[id] = k;
		for(j=0;j<k;j++)
			scanf("%d",&a[id][j]);
	}

	for(i=0;i<100;i++)
		level[i] = 0;

	dfs(1,0);

	printf("%d",level[0]);

	for(i=1;i<=maxlevel;i++)
		printf(" %d",level[i]);

	printf("\n");
	return 0;
}

int dfs(int num,int l)
{
	int i;
	if(b[num] == 0)
	{
		level[l] += 1;
		if(l > maxlevel)
			maxlevel = l;
		return ;
	}

	for(i=0;i<b[num];i++)
		dfs(a[num][i],l+1);
}

提交了之后一次就AC了!!前面纠结了这么就没想到这么快就搞定了。其实这一题用宽度搜索并不好,每一层的边界不好控制,而且最后搜索完跳出来时还纠结了一会。队列的开销也导致有个case不通过。。。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值