4 3 1 2 2 3 4 3
1 2 4 3
AC-code:
#include<cstdio> #include<cstring> int map[510][510],queue[510],indegree[510]; void topo(int n) { int t=0,k,i,j; for(j=0;j<n;j++) { for(i=1;i<=n;i++) if(indegree[i]==0) { k=i; break; } queue[t++]=k; indegree[k]=-1; for(i=1;i<=n;i++) if(map[k][i]) indegree[i]--; } printf("%d",queue[0]); for(i=1;i<n;i++) printf(" %d",queue[i]); printf("\n"); } int main() { int m,a,n,b; while(scanf("%d%d",&n,&m)!=EOF) { memset(map,0,sizeof(map)); memset(indegree,0,sizeof(indegree)); while(m--) { scanf("%d%d",&a,&b); if(map[a][b]==0) { indegree[b]++; map[a][b]=1; } } topo(n); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/lin14543/article/details/47659257