Question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1285
Question
Time Limit: 2000/1000 ms (Java/other) memory limit: 65536/32768 K (Java/other) total submission (s): 35 accepted submission (s ): 14 Problem description has n game teams (1 <= n <= 500), numbered 1, 2, 3 ,...., n. After the competition is over, the referee committee will rank all participating teams from the past to the next. However, the referee Committee cannot directly obtain the results of each team, but only knows the results of each competition, that is, P1 wins P2, which is represented by P1 and P2, and is ranked before P2. Now, compile the program to determine the ranking.
The input has several groups. The first behavior in each group is n (1 <= n <= 500) and M. N indicates the number of groups, M indicates that there are m rows of input data. In the next row of M data, each row also has two integers P1. P2 indicates that the P1 team won the P2 team.
Output provides a qualified ranking. There is a space between the output team numbers, and there is no space behind the last one.
Other note: the qualified ranking may not be unique. In this case, the team with a small number must be in front of the output. The input data must be correct, that is, input data to ensure a qualified ranking.
Sample Input
4 31 22 34 3
Sample output
1 2 4 3 Topology Sorting all in the code !!! AC code:# Include <stdio. h> # include <string. h> int map [505] [505]; // map [I] [J] indicates whether I win J is true int s [505]; // s [I] indicates the number of times the team I lost. Int main () {// freopen ("in.txt", "r", stdin); int n, m, I, j, X, Y; while (scanf ("% d", & N, & M )! = EOF) {memset (S, 0, sizeof (s); memset (MAP, 0, sizeof (MAP); for (I = 1; I <= m; I ++) {scanf ("% d", & X, & Y); If (! Map [x] [Y]) // be careful !!! Have you ever input {map [x] [Y] = 1; s [y] ++ ;}} x = 0; while (x <n) // n cycles output {If (x) printf (""); for (I = 1; I <= N; I ++) // find the point with a small number and an inbound value of 0 {If (s [I] = 0) {printf ("% d", I ); s [I] =-1; // The break will not be accessed again next time;} For (j = 1; j <= N; j ++) if (Map [I] [J] = 1) // deletes all I-related edges. s [J] --; X ++ ;} printf ("\ n");} return 0 ;}