原题连接:https://www.patest.cn/contests/pat-a-practise/1004
题目如下:
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,简单的用一个二维数组Node[ID][v](是1的话)表示ID的孩子是v,不断更新tail直到该层的结尾,然后将last指向该层的最后一个元素
当从堆栈中弹出的元素和last相等的时候,说明一层的遍历结束,进行输出操作、层数的增加、计数的置0等。直到队为空。
然而,有两个case过不去(一个8分,一个3分),我实在是想不到哪里错了,自己也想不出可以用来测试的样例,希望有大神能帮忙解决下,不胜感激!代码如下:
#include<stdio.h> #include<stdbool.h> #include<stdlib.h> #define Max 105 //以下是队列的一些操作 typedef struct QNode{ int Maxsize; int rear; int front; int *Data; }Quenue; Quenue *CreatQ(int N) { Quenue *Q=(Quenue *)malloc(sizeof (struct QNode)); Q->Data=(int *)malloc((N+1)*sizeof(int)); Q->rear=Q->front=0; Q->Maxsize=N+1; return Q; } bool IsFull(Quenue *Q) { return ((Q->rear+1)%(Q->Maxsize)==Q->front); } void Add(Quenue *Q,int X) { if(!IsFull(Q)) { Q->rear=(Q->rear+1)%(Q->Maxsize); Q->Data[Q->rear]=X; } } bool IsEmpty(Quenue *Q) { return (Q->front==Q->rear); } int Delete(Quenue *Q) { if (!IsEmpty(Q)) { Q->front=(Q->front+1)%(Q->Maxsize); return Q->Data[Q->front]; } } // int main() { int N,M,i,j,K,ID,v; int last,tail,level=0; int Node[Max][Max]={0}; int Visited[Max]={0}; scanf("%d %d",&N,&M); Quenue *Q=CreatQ(N); for (i=0;i<M;i++) { scanf("%d %d",&ID,&K); if (i==0) { Visited[ID]=1; //将第一个数入队 Add(Q,ID); last=tail=ID; } for (j=0;j<K;j++) { scanf("%d",&v); Node[ID][v]=1; //确定ID的孩子v } } int Number[Max]={0}; int w,put=0,flag=0; while (!IsEmpty(Q)) { w=Delete(Q); flag=0; for (i=1;i<=N;i++) { if(i==w)continue; if (!Visited[i]&&Node[w][i]==1) { flag=1; //不是叶节点 Visited[i]=1; Add(Q,i); tail=i; } } //w的所有孩子都遍历完 if (flag==0)Number[level]++; //说明w是叶节点 if (w==last) //一层结束 { if (put==0){printf("%d",Number[level]);put=1;} else printf(" %d",Number[level]); level++; last=tail; } } return 0; }
在网上看了其他人的解法,发现这个DFS解法挺妙的:
1 #include<stdio.h> 2 int mat[105][105]={0}; 3 int num[105]={0}; 4 int MaxDeep=-1; 5 int n,m; 6 7 void dfs(int id,int deep) 8 { 9 int leaf=1; 10 int i; 11 for (i=1;i<=n;i++) 12 { 13 if (mat[id][i]){leaf=0;dfs(i,deep+1);} 14 } 15 if (leaf) 16 { 17 num[deep]++; 18 if (deep>MaxDeep)MaxDeep=deep; 19 } 20 } 21 22 int main() 23 { 24 scanf("%d %d",&n,&m); 25 int i,j; 26 for (i=0;i<m;i++) 27 { 28 int father,k,child; 29 scanf("%d %d",&father,&k); 30 for (j=0;j<k;j++) 31 { 32 scanf("%d",&child); 33 mat[father][child]=1; 34 } 35 } 36 dfs(1,0); 37 int flag=0; 38 for (i=0;i<=MaxDeep;i++) 39 { 40 if (!flag){printf("%d",num[i]);flag=1;} 41 else printf(" %d",num[i]); 42 } 43 return 0; 44 }