标签:
题目链接:HDU - 4857
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 #include<stack> 10 #define inf 0x7fffffff 11 using namespace std; 12 const int maxn=30000+10; 13 14 int n,m; 15 vector<int> vec[maxn]; 16 int vis[maxn],flag; 17 int oud[maxn]; 18 priority_queue<int,vector<int>,less<int> > Q; 19 stack<int> S; 20 21 int main() 22 { 23 int t; 24 scanf("%d",&t); 25 while (t--) 26 { 27 scanf("%d%d",&n,&m); 28 for (int i=1 ;i<=n ;i++) vec[i].clear(); 29 memset(vis,0,sizeof(vis)); 30 memset(oud,0,sizeof(oud)); 31 int a,b; 32 for (int i=0 ;i<m ;i++) 33 { 34 scanf("%d%d",&a,&b); 35 vec[b].push_back(a); 36 oud[a]++; 37 } 38 flag=0; 39 while (!Q.empty()) Q.pop(); 40 while (!S.empty()) S.pop(); 41 for (int i=1 ;i<=n ;i++) if (!oud[i]) Q.push(i); 42 while (!Q.empty()) 43 { 44 int u=Q.top() ;Q.pop() ; 45 S.push(u); 46 for (int i=0 ;i<vec[u].size() ;i++) 47 { 48 int v=vec[u][i]; 49 oud[v]--; 50 if (oud[v]==0) Q.push(v); 51 } 52 } 53 while (!S.empty()) 54 { 55 int u=S.top() ;S.pop() ; 56 if (!flag) flag=1; 57 else printf(" "); 58 printf("%d",u); 59 } 60 printf("\n"); 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/huangxf/p/4470579.html