标签:杭电 tle tput fir 条件 for ace ane accept
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33762 Accepted Submission(s): 13236
1 //2018年5月27日 11:26:43 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 using namespace std; 7 8 const int N = 500; 9 const int M = 1001; 10 11 int n, m; 12 int inDeg[N]; 13 14 int fir[N], cnt; 15 struct Edge{ 16 int nxt; 17 int to; 18 }E[M]; 19 20 void addEdge(int x, int y){ 21 E[++cnt].to = y; 22 E[cnt].nxt = fir[x]; 23 fir[x] = cnt; 24 } 25 26 void Topsort(){ 27 priority_queue<int, vector<int>, greater<int> > q; 28 for(int i=1; i<=n; i++) 29 if(inDeg[i] == 0) 30 q.push(i); 31 int flag = 0; 32 while(!q.empty()){ 33 int v = q.top(); 34 q.pop(); 35 if(flag) printf(" "); 36 printf("%d", v); 37 flag = 1; 38 for(int i=fir[v]; i; i=E[i].nxt){ 39 inDeg[E[i].to]--; 40 if(!inDeg[E[i].to]) q.push(E[i].to); 41 } 42 } 43 printf("\n"); 44 } 45 46 int main(){ 47 while(scanf("%d%d", &n, &m) != EOF){ 48 memset(E, 0, sizeof(E)); cnt = 0; 49 memset(inDeg, 0, sizeof(inDeg)); 50 memset(fir, 0, sizeof(fir)); 51 for(int i=1; i<=m; i++){ 52 int x, y; 53 scanf("%d%d", &x, &y); 54 addEdge(x, y); 55 inDeg[y]++; 56 } 57 Topsort(); 58 } 59 60 return 0; 61 }
标签:杭电 tle tput fir 条件 for ace ane accept
原文地址:https://www.cnblogs.com/sineagle/p/9097886.html