标签:des style blog http color io os ar java
http://acm.hdu.edu.cn/showproblem.php?pid=1285
多种方法求拓扑排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12796 Accepted Submission(s): 5139
#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; int d[505],map[505][505]; priority_queue<int,vector<int>,greater<int> > q; void toposort(int n) { int v,res[505],r,i; r=0; for(i=1;i<=n;i++) { if(d[i]==0) q.push(i); } while(!q.empty()) { v=q.top(); // printf("top=%d\n",top); q.pop(); res[r++]=v; for(i=1;i<=n;i++) { if(!map[v][i]) continue; d[i]--; if(d[i]==0) q.push(i); } } printf("%d",res[0]); for(i=1;i<r;i++) printf(" %d",res[i]); printf("\n"); } int main() { int m,x,y,n,i; while(~scanf("%d%d",&n,&m)) { memset(d,0,sizeof(d)); memset(map,0,sizeof(map)); for(i=0;i<m;i++) { scanf("%d%d",&x,&y); if(map[x][y]) continue; map[x][y]=1; d[y]=d[y]++; } toposort(n); } }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/cancangood/p/4014680.html