标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17184 Accepted Submission(s): 6827
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int map[550][550], indegree[550], result[550]; 6 int n, m; 7 void Tsort(){ 8 int m, k = 0; 9 for(int i = 0; i < n; i++){ 10 //*m = -1; 11 for(int j = 1; j <= n; j++){ 12 if(!indegree[j]){ 13 m = j; break; 14 } 15 } 16 result[k++] = m; indegree[m]--; //删点; 19 for(int j = 1; j <= n; j++) //删边; 20 if(map[m][j]) 21 --indegree[j]; 22 } 23 for(int i = 0; i < k; i++){ 24 if(i != 0) 25 printf(" %d", result[i]); 26 else 27 printf("%d", result[i]); 28 } 29 printf("\n"); 30 } 31 int main() 32 { 33 while(~scanf("%d %d", &n, &m)) 34 { 35 memset(map, 0, sizeof(map)); 36 memset(indegree, 0, sizeof(indegree)); 37 for(int i = 0; i < m; i++) 38 { 39 int a, b; 40 scanf("%d %d", &a, &b); 41 if(!map[a][b]){ //去重; 42 map[a][b] = 1; 43 indegree[b]++; 44 } 45 } 46 Tsort(); 47 } 48 return 0; 49 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4729384.html