4 3 1 2 2 3 4 3
1 2 4 3
#include <stdio.h> #include <string.h> #define MAX 510 int graph[MAX][MAX] , in[MAX] ; bool visited[MAX] ; int main() { int n , m ; while(scanf("%d%d",&n,&m) != EOF) { memset(in,0,sizeof(in)) ; memset(graph,0,sizeof(graph)); memset(visited,false,sizeof(visited)); for(int i = 0 ; i < m ; ++i) { int x , y ; scanf("%d%d",&x,&y); if(graph[x][y] == 0) //防止重边 { graph[x][y] = 1 ; in[y]++ ; } } for(int i = 0 ; i < n ; ++i) { int ans = 0 ; for(int j = 1 ; j <= n ; ++j) { if(!visited[j] && in[j] == 0) { ans = j ; visited[j] = true ; break ; } } for(int j = 1 ; j <= n ; ++j) { if(graph[ans][j] == 1) { in[j]--; graph[ans][j] = 0 ; } } printf("%d",ans); if(i != n-1) { printf(" ") ; } } printf("\n") ; } return 0 ; }
hdu 1285 确定比赛名次 简单的拓扑排序模板题~~需要注意重边
原文地址:http://blog.csdn.net/lionel_d/article/details/43883675