9 degree question 1449: Determine the competition rankings, 9 degrees 1449
Obviously, the Topology Sorting problem will be found to be easy to understand after drawing a graph. At the beginning, I did not quite understand it myself. After drawing a graph, the idea is very clear.
Reprinted please indicate this article address http://blog.csdn.net/yangnanhai93/article/details/41226221
Question link http://ac.jobdu.com/problem.php? Pid = 1, 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****************************************************************/