标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19100 Accepted Submission(s): 7610
1 #include <cstdio> 2 #include <vector> 3 #include <functional> 4 #include <queue> 5 using namespace std; 6 bool cmp(const int &x,const int &y) 7 { 8 return x>y; 9 } 10 11 const int maxn=550; 12 vector<int> v[maxn]; 13 int indegree[maxn]; 14 priority_queue<int,vector<int>,greater<int> >q; 15 queue<int> s; 16 int main() 17 { 18 int n,m,a,b; 19 while(scanf("%d%d",&n,&m)==2) 20 { 21 for(int i=1;i<=n;i++) indegree[i]=0; 22 for(int i=0;i<m;i++) 23 { 24 scanf("%d%d",&a,&b); 25 v[a].push_back(b); 26 indegree[b]++; 27 } 28 for(int i=1;i<=n;i++) 29 if(indegree[i]==0) q.push(i); 30 while(!q.empty()) 31 { 32 int t=q.top(); q.pop(); 33 for(int i=0;i<v[t].size();i++) 34 { 35 indegree[v[t][i]]--; 36 if(indegree[v[t][i]]==0) q.push(v[t][i]); 37 } 38 s.push(t); 39 } 40 int first=true; 41 while(!s.empty()) 42 { 43 if(first){printf("%d",s.front()); first=false;} 44 else printf(" %d",s.front()); 45 s.pop(); 46 } 47 printf("\n"); 48 for(int i=1;i<=n;i++) v[i].clear(); 49 } 50 51 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5185070.html