determine the position of the match
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 13215 Accepted Submission (s): 5291
Problem Description
There are N teams (1<=n<=500), numbered three-in-one, .... , N to play, after the game, the Referee Committee will be all the teams from the arrival of the ranking, but now the referee committee can not directly get each team's performance, only know the results of each game, that is, P1 win P2, with P1,p2 said, ranked P1 before P2. Now ask you to compile the program to determine the rankings.
Input
The input has several groups, the first behavior in each group is two number n (1<=n<=500), M, where n represents the number of troops, and m indicates the input data for the M row. In the next M-row data, there are also two integers per line p1,p2 means that the P1 team won the P2 team.
Output
Give a ranking that meets the requirements. There is a space between the queue numbers at the time of the output, and no space after the last.
Other Notes: Qualifying rankings may not be unique, at which point the output is required to be numbered in front of the team; the input data is guaranteed to be correct, i.e. the input data ensures that there must be a qualifying ranking.
Sample Input
4 3
1 2
2 3
4 3
Sample Output
1 2 4 3
Idea: The priority queue in STL is used because the topological ordering of dictionary order is satisfied.
Priority_queue<int,vector<int>, greater<int> > Q;
It realizes the high priority of the weight, and ensures that the number is the smallest in the queue when it is taken out.
Other and general topological sorting is no different.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < Queue> #include <vector>using namespace std;const int maxn = 550;const int MAXM = 25000;int Head[maxn],indegree[ma xn],ans[maxn],n,m;struct edgenode{int to; int W; int next;}; Edgenode edges[maxm];int Toposort () {priority_queue<int,vector<int>, greater<int> > Q; for (int i = 1; I <= N; i++) {if (indegree[i]==0) Q.push (i); } int id = 0; while (! Q.empty ()) {int u; ans[id++] = U = q.top (); Q.pop (); for (int k = Head[u]; K! =-1; k = edges[k].next) {indegree[edges[k].to]--; if (!indegree[edges[k].to]) {Q.push (edges[k].to); }}} if (id = = N) {for (int i = 0; i < ID; i++) {if (i! = id-1) cout << Ans[i] << ""; else cout << Ans[i] << Endl; }} else return 0;} int main () {int x, y; while (Cin >> N >> M) {memset (ans,0,sizeof (ans)); memset (head,-1,sizeof (head)); memset (edges,0,sizeof (Edges)); for (int i = 0; i < M; i++) {cin >> x >> y; edges[i].to = y; EDGES[I].W = 1; Edges[i].next = Head[x]; HEAD[X] = i; indegree[y]++; } toposort (); } return 0;}
HDU1285 determining the tournament position "topology sort" "Priority queue"