拓扑排序模版--hdu 1285 确定比赛名次
1 #include <stdio.h> 2 #include <string.h> 3 int count,n,degree[505],map[505][505],vis[505],res[505]; 4 5 void topo(int x) 6 { 7 res[count] = x; 8 count++; 9 int i; 10 for(i = 1;i <= n;i++) 11 { 12 if(map[x][i] && !vis[i]) 13 degree[i]--; 14 } 15 } 16 17 int main() 18 { 19 int a,b,m,i; 20 while(~scanf("%d %d",&n,&m)) 21 { 22 memset(map,0,sizeof(map)); 23 memset(degree,0,sizeof(degree)); 24 memset(vis,0,sizeof(vis)); 25 for(i = 0;i < m;i++) 26 { 27 scanf("%d %d",&a,&b); 28 if(map[a][b] == 0) 29 { 30 map[a][b] = 1; 31 degree[b]++; 32 } 33 } 34 count = 0; 35 while(count < n) 36 { 37 for(i = 1;i <= n;i++) 38 { 39 if(!vis[i] && !degree[i]) 40 { 41 topo(i); 42 vis[i] = 1; 43 break; 44 } 45 } 46 } 47 for(i = 0;i < n-1;i++) 48 printf("%d ",res[i]); 49 printf("%d\n",res[i]); 50 } 51 return 0; 52 }