思路:拓扑排序
#include<stdio.h> #include<stdlib.h> typedef struct vert { int num; //结点编号 struct vert* next; }G_v; typedef struct node { int InDgree,OutDgree; //出度和入度 G_v* next; }G_n; G_n P[101]; //结点 int main(void) { G_v *tmp; G_v *q; int n,i,a,j,count; scanf("%d",&n); for(i = 0;i <= n;i ++) { P[i].next = NULL; P[i].InDgree = P[i].OutDgree = 0; } i = 1; while(i <= n) { tmp = P[i].next; while(scanf("%d",&a)&&a != 0) { P[i].OutDgree ++; P[a].InDgree ++; q = (G_v*)malloc(sizeof(G_v)); q->num = a; q->next = NULL; if(tmp == NULL) { P[i].next = q; tmp = P[i].next; } else { tmp->next = q; tmp = tmp->next; } } i ++; } count = 1; for(i = 1;i <= n;i ++) { if(P[i].InDgree == 0) { if(count++ == n) { printf("%d\n",i); break; } else printf("%d ",i); tmp = P[i].next; while(tmp) { P[tmp->num].InDgree --; tmp = tmp->next; } P[i].InDgree = 200; i = 0; } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/ibigprogramer/article/details/47205923