标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2257 Accepted Submission(s):
637
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 int head[100010], indegree[100010], result[100010]; 7 int n, m; 8 9 struct rode{ 10 int to, next; 11 } map[100010]; 12 void Tsort(int n) 13 { 14 priority_queue<int> q; int k = 1, u; 15 for(int i = 1; i <= n; i++) 16 if(!indegree[i]) 17 q.push(i); 18 while(!q.empty()) 19 { 20 result[k++] = u = q.top(); 21 q.pop(); 22 for(int i = head[u]; i != -1; i = map[i].next) 23 { 24 if(!--indegree[map[i].to]) 25 q.push(map[i].to); 26 } 27 } 28 for(int i = n; i >= 1; i--) 29 { 30 if(i != 1) 31 printf("%d ", result[i]); 32 else 33 printf("%d\n", result[i]); 34 35 } 36 } 37 int main() 38 { 39 int t; 40 scanf("%d", &t); 41 while(t--) 42 { 43 memset(indegree, 0, sizeof(indegree)); 44 memset(head, -1, sizeof(head)); 45 scanf("%d %d", &n, &m); 46 for(int i = 0; i < m; i++) 47 { 48 int a, b; 49 scanf("%d %d", &a, &b); 50 map[i].to = a; 51 map[i].next = head[b]; 52 head[b] = i; 53 ++indegree[a]; 54 } 55 Tsort(n); 56 } 57 return 0; 58 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4728843.html