[Bestcoder round #3 is coming !] August 3 ~ (Registration stops 30 minutes before the competition) |
Determine competition rankingsTime Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 11642 accepted submission (s): 4634
Problem description has n 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 input4 31 22 34 3 Sample output1 2 4 3 Authorsmallbeer (CRF) Source hangdian ACM training team training competition (VII) |
Question Analysis: This is the basic topic for Topology Sorting!
First, we will introduce what is topological sorting and arrange a pile of data elements in a certain link order.
Think of every variable as a vertex and a directed graph as a directed edge. Our task is to know that the computer sorts all nodes in a graph.
In graph theory, this problem is called topological sorting.
Algorithm Analysis: Topology Sorting is well understood based on the concept of the adjacent table, and the specific implementation is more difficult.
You can also use the adjacent matrix to process the data. The algorithm is as follows:
Create a map adjacent matrix and assign the initial value (not 1 ). Create a special processing array that is not as good as in [N]; this array is used to remember the number of times, initially assigned 0. If it is a 1-> 2 relationship, then
In [2] ++; and 3 also-> 2, then add, that is, in [pointing to the target] ++;
Open a storage stack to store data that is pushed into the stack at a time! Create a toposort function to process in [] and map [] []. Loop traversal of N (number of elements) times, each time an element enters the storage stack.
In each large loop, if the element in [] is traversed for the in [] array, whether it is 0. If it is 0, it indicates that it is a node without an inbound degree, and the side puts it into the stack. And put in []-1.
Then ......
The Code is as follows: (read this algorithm carefully)
# Include <stdio. h>
# Include <string. h>
Int map [502] [502];
Int in [502];
Int s [502], K;
Int n, m;
Void tpsort ()
{
Int m;
M = N;
Int I, J;
While (M --)
{
For (I = 1; I <= N; I ++)
{
If (in [I] = 0)
{
S [k ++] = I;
In [I]-= 1;
For (j = 1; j <= N; j ++)
{
If (Map [I] [J] = 1)
{
In [J]-= 1;
}
}
Break;
}
}
}
For (I = 0; I <K; I ++)
{
Printf ("% d % C", s [I], I = K-1? '\ N ':'');
}
}
Int main ()
{
Int I, J;
Int A, B;
While (scanf ("% d", & N, & M )! = EOF)
{
K = 0;
For (I = 1; I <= N; I ++)
{
For (j = 1; j <= N; j ++)
{
Map [I] [J] =-1;
}
}
Memset (in, 0, sizeof (in ));
For (j = 0; j <m; j ++)
{
Scanf ("% d", & A, & B );
If (Map [a] [B] =-1)
{
Map [a] [B] = 1;
In [B] ++;
}
}
Tpsort ();
}
Return 0;
}