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 31 22) 34 3
Sample Output
1 2 4 3
---------------------------------------------------I'm the split line ^_^---------------------------------------------------- ------
This problem, is a topological sort of entry, in fact, my first topological sort is done on the Hiho, than this to come to the simple, so
I chose this question to record, the problem is mainly a template, remember to spend a little time to understand the good, there is a point, the problem is used
Priority Queue Priority_queue, here give two kinds of STL priority criteria, small elements first: <int, Vector<int>, greater<int>;
There are big elements first: <int, Vector<int>, and greater<int>, which is useful in terms of dictionary size output, remember.
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>UsingNamespace Std;#define INT __int64#define INF0x3f3f3f3fConstint MAXN=555;int team[MAXN];int Head[MAXN];int point[MAXN];int NXT[MAXN];int edgecnt;BOOL Vis[MAXN][maxn];int Ingrade[MAXN];voidIni(){Memset(Team,0,sizeof(Team));Memset(Head,-1,sizeof(Head));Memset(Point,-1,sizeof(Point));Memset(NXT,-1,sizeof(NXT));Memset(Vis,0,sizeof(Vis));Memset(Ingrade,0,sizeof(Ingrade)); Edgecnt=0;}voidAdd_edge(int U,Int V){edgecnt++; Nxt[edgecnt]= Head[u]; Point[edgecnt]= V; Head[u]= edgecnt;}IntMain(){Freopen ("Input.txt", "R", stdin);int nM;While(scanf("%d%d",&n,&m)! = EOF){Ini();int aB;int ans[MAXN]={0};While(M--){scanf("%d%d",&a,&b);If(Vis[A][b])Continue;else Vis[A][b]=True;Add_edge(AB); Ingrade[b]++;}int k=0; Priority_queue<Int, Vector<Int, Greater<Int>>q;While(!q.Empty()) Q.Pop();For(int I=1; I<= N; I++){If(Ingrade[I]==0) Q.Push(I);}While(!q.Empty()){int now= Q.Top(); Q.Pop(); Ans[k++]= Now;For(int I= Head[Now]; I!=-1; I= NXT[I]){Int V= Point[I]; Ingrade[V]--;If(!ingrade[V]) Q.Push(V);}}For(int I=0; I< K; I++){Printf("%d", ans[Iif (i != k -1) {printf else { Printf ( "\n "); } } return 0;}
Topology sequencing-determining the tournament position