Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 959 Accepted Submission(s): 284
1 #include<string> 2 #include<cstdio> 3 #include<iostream> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #include<cstring> 9 #include<stdlib.h> 10 #include<string> 11 #include<cmath> 12 using namespace std; 13 #define pb push_back 14 vector<int >p[30010]; 15 int in[30010],n,m,deep[10010],num[10010]; 16 char dp[2010][2010]; 17 void init(){ 18 memset(in,0,sizeof(in)); 19 memset(deep,0,sizeof(deep)); 20 memset(num,0,sizeof(num)); 21 for(int i=0;i<=n;i++) p[i].clear(); 22 } 23 int tuopu(){ 24 priority_queue<int >ak_47; 25 stack<int >ko; 26 int cnt=0; 27 for(int i=1;i<=n;i++) 28 if(in[i]==0) ak_47.push(i); 29 while(!ak_47.empty()){ 30 int pos=ak_47.top(); 31 ko.push(pos); 32 ak_47.pop(); 33 for(int i=0;i<p[pos].size();i++){ 34 int to=p[pos][i]; 35 if((--in[to])==0) ak_47.push(to); 36 } 37 38 } 39 while(!ko.empty()){ 40 if(cnt) printf(" "); 41 cnt=1; 42 printf("%d",ko.top()); 43 ko.pop(); 44 } 45 printf("\n"); 46 } 47 int main(){ 48 int cas=0,t; 49 cin>>t; 50 while(t--){ 51 cin>>n>>m; 52 init(); 53 for(int i=1;i<=m;i++){ 54 int a,b;scanf("%d%d",&a,&b); 55 in[a]++,p[b].pb(a); 56 } 57 tuopu(); 58 } 59 }
原文:http://www.cnblogs.com/ainixu1314/p/3896017.html