标签:print amp course std str div turn fill color
C\C++:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <climits> 9 #include <iostream> 10 #include <algorithm> 11 #define INF 0xffffff 12 using namespace std; 13 14 const int my_max = 20010; 15 int n, m, a, b, my_cnt, my_indeg[my_max]; 16 vector <int> my_G[my_max]; 17 18 int topsort() 19 { 20 my_cnt = 0; 21 int my_ans = 0, t = 887; 22 queue <int> Q; 23 while (t ++) 24 { 25 int flag = -1, my_temp = 0; 26 for (int i = 1; i <= n; ++ i) 27 if (my_indeg[i] == 0) 28 { 29 my_indeg[i] = -1; 30 flag = 1; 31 Q.push(i); 32 my_cnt ++; 33 my_temp ++; 34 } 35 36 if (flag == -1) break; 37 my_ans += my_temp * t; 38 39 while (my_temp --) 40 { 41 int my_now = Q.front(); 42 for (int i = 0; i < my_G[my_now].size(); ++ i) 43 my_indeg[my_G[my_now][i]] --; 44 my_G[my_now].clear(); 45 Q.pop(); 46 } 47 } 48 if (my_cnt == n) 49 return my_ans; 50 51 for (int i = 1; i <= n; ++ i) 52 my_G[i].clear(); 53 return -1; 54 } 55 56 int main() 57 { 58 while (~scanf("%d%d", &n, &m)) 59 { 60 memset(my_indeg, 0, sizeof(my_indeg)); 61 while (m --) 62 { 63 scanf("%d%d", &a, &b); 64 ++ my_indeg[a]; 65 my_G[b].push_back(a); 66 } 67 68 printf("%d\n", topsort()); 69 } 70 return 0; 71 }
标签:print amp course std str div turn fill color
原文地址:https://www.cnblogs.com/GetcharZp/p/9458015.html