标签:blog http io ar os sp for on 2014
很明显的拓扑排序问题,绘制成图之后会发现很好理解,刚开始我自己也不是很理解,画图之后思路特别清晰
转载请注明本文地址http://blog.csdn.net/yangnanhai93/article/details/41226221
题目链接http://ac.jobdu.com/problem.php?pid=1449
#include <iostream> #include <memory.h> #include <queue> using namespace std; int A[501],MapCount[501][501]; void TopoSort(int n) { priority_queue<int,vector<int>,greater<int> > qu; vector<int>result; for(int i=1;i<=n;i++) if(A[i]==0) qu.push(i); int count=0; while(!qu.empty()) { int front=qu.top(); qu.pop(); result.push_back(front); count++; for (int i=1;i<=n;i++) { if(MapCount[front][i]) { A[i]--; if(A[i]==0) qu.push(i); } } } cout<<result[0]; for (int i=1;i<result.size();i++) { cout<<" "<<result[i]; } cout<<endl; } int main() { //freopen("data.in","r",stdin); int m,n,x,y; while(cin>>m>>n&&m) { memset(A,0,sizeof(A)); memset(MapCount,0,sizeof(MapCount)); for(int i=0;i<n;i++) { cin>>x>>y; MapCount[x][y]=1; } for(int i=1;i<=m;i++) for(int j=1;j<=m;j++) if(MapCount[i][j]!=0) A[j]++; TopoSort(m); } return 0; } /************************************************************** Problem: 1449 User: vincent_ynh Language: C++ Result: Accepted Time:30 ms Memory:2500 kb ****************************************************************/
标签:blog http io ar os sp for on 2014
原文地址:http://blog.csdn.net/yangnanhai93/article/details/41226221