1004. Counting Leaves (30)
来源:互联网 发布:c语言学生管理系统v5 编辑:程序博客网 时间:2024/04/28 07:38
PAT题目:
1004. Counting Leaves (30)
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 Input2 101 1 02Sample Output
0 1
题目看起来很长,实际上就是求一个树每一层的叶子数目。求每一层的叶子数目,最先想到的就是宽度优先搜索——BFS,于是就通过队列写了一个BFS搜索来计算每一层数目的多少:
#include <stdio.h>#define MAXSIZE 200typedef 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;elsenum += 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不通过。。。
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 学习中反思
- AutoResetEvent与ManualResetEvent的区别
- Linux apache + fastcgi 运行c/c++
- Linux之CentOS-6.3下部属SVN服务器整理
- 实现表格边框细线化的方法
- 1004. Counting Leaves (30)
- 用python编写maya插件
- Mac下Myeclipse编辑HTML中文乱码;或许对相似问题也有帮助
- Check if array element appears on line
- Spring ApplicationContextAware 获取bean
- 第三章 最简单的C程序设计——顺序程序设计
- hdu 5040 BFS 多维化处理图
- C++获取系统时间的方法
- fragment