Determine the position of the matchTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 16487 Accepted Submission (s): 6528
Problem description has 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 inputs have several groups, the first behavior in each group is two n (1<=n<=500), M, where n represents the number of troops, and m represents 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 gives 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
The first topological sort of problem, it may be a bit cumbersome to write, but the comments are easy to read,
#include <stdio.h> #include <string.h> #include <queue>using namespace std; #define M 510int N,m;int mp[ M][M],RE[M],DU[M],VIS[M];//MP is whether the storage has a relationship, re is the result of the sorting, Vis is the mark is visited, Du is the entry of the record point void Topo () {memset (vis,0,sizeof (VIS)); int T=0,i;queue<int> q;for (i=1;i<=n;i++) if (du[i]==0) {Q.push (i); break;//find the first point with a smaller value and a 0 in, enter the queue}while (! Q.empty ()) {int Tmp=q.front (); Q.pop (); re[t++]=tmp;vis[tmp]=1;//tag this point has been accessed for (i=1;i<=n;i++) {if (mp[tmp][i]==1) { du[i]--;//deletes the edge with this point as the starting point, updating the}}for (i=1;i<=n;i++) if (!vis[i]&&du[i]==0) {Q.push (i) of the point connected to the dot; break;// Find the next smaller and 0 point of the queue}}printf ("%d", re[0]);//output point for (i=1;i<t;i++) printf ("%d", Re[i]);p rintf ("\ n");} int main () {int i,a,b;while (scanf ("%d%d", &n,&m)!=eof) {memset (Mp,0,sizeof (MP)); Memset (du,0,sizeof (du)); for (i=0;i<m;i++) {scanf ("%d%d", &a,&b), if (!mp[a][b]) {mp[a][b]=1;du[b]++;}} Topo ();} return 0;}
HDU 1285 determining the tournament position (topological sorting)