In this way, the topological sorting of water will time out, cry, and the variables defined in it will actually time out. It's right to take it out, and, the sentence must be harsh!
Description
There are 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. Compile Program Determine the ranking.
Input
There are several groups in the input. 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
Provide 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
this topic is a typical topic in topological sorting. It is easy to output according to the size, which is exactly the same as the traversal order.
# Include <stdio. h> # include <string. h >#include <iostream> # define n 550 # define M 2100 using namespace STD; int map [N] [N], in [m], F [m]; int main () {int I, j, t, n, m, a, B, num; while (~ Scanf ("% d", & N, & M) {memset (MAP, 0, sizeof (MAP); memset (in, 0, sizeof (in); While (M --) {scanf ("% d", & A, & B ); if (Map [a] [B] = 0) {map [a] [B] = 1; in [B] ++ ;}} num = 0; while (num! = N) {for (I = 1; I <= N; I ++) if (in [I] = 0) {in [I] =-1; f [num ++] = I; t = I; break;} For (j = 1; j <= N; j ++) if (Map [T] [J] = 1) in [J] --;} for (I = 0; I <n-1; I ++) printf ("% d", F [I]); printf ("% d \ n", F [n-1]);} return 0 ;}