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 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
Spiritual Journey
The pit of the newly opened topological sort is found to be quite simple, the entry of each point is recorded at the time of reading, and then the point is continuously found at 0, which is thrown into another array, minus the edge that it goes to another point, that is, the entry of each point it reaches-1. Until all the numbers are loaded into the array, then the array is output.
Code:
#include <cstdio> #include <iostream> #include <cstring> #define N 505using namespace Std;int a[n][n],b [N],c[n],i,j,s,e,n,m;int Main () {while (scanf ("%d%d", &n,&m)!=eof) {memset (a,0,sizeof (a)); Memset (b,0,sizeof (b)); Memset (C,0,sizeof (c)), for (i=1;i<=m;i++) {scanf ("%d%d", &s,&e), if (!a[s][e]) {a[s][e]=1;b[e]++;}} int Num=0;while (1) {J=0;while (1) {j++;if (b[j]==0) {break;} if (j>n) {break;}} B[j]=-1; C[++num]=j; for (int k=1;k<=n;k++) { if (a[j][k]>0) { b[k]--; A[j][k]=-1;}} if (num>n) {break;} } for (i=1;i<=n-1;i++) {printf ("%d", C[i]);} printf ("%d\n", C[n]);} return 0;}
Daily water problem, body stick!
hdu1285 determining the rank of the match (topological sort)