http://acm.hdu.edu.cn/showproblem.php?pid=1285
determine the position of the match
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14739 Accepted Submission (s): 5892
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 INPUT4 3 Sample Output 1 2 4 3 analysis; topological sorting algorithm.
AC Code:
1 //A typical topological sorting algorithm (adjacency array form) that can be used as a template for topological sequencing2#include <iostream>3 //#include <conio.h>4 using namespacestd;5 #defineArraySize 5016 intMap[arraysize][arraysize];//a critical array of storage graphs7 intn,m;8 intIndegree[arraysize];//the degree of Storage point entry9 intMain ()Ten { One inti,j,k; A intp1,p2; - //freopen ("1.txt", "R", stdin); - while(cin>>n>>m) the { -memset (Map,0,sizeof(map)); -memset (Indegree,0,sizeof(Indegree)); - for(i=0; i<m;++i) + { -Cin>>p1>>P2; + if(!MAP[P1][P2])//don't forget the judgment of the heavy side here, otherwise it will WA A { atMAP[P1][P2] =1; -indegree[p2]++;//in the case of a heavy edge, if you do not process, the degree is calculated incorrectly - } - } - //Topological sorting - for(i=1; i<n+1; ++i)//perform n traversal to find a node with an entry level of 0 each time in { - for(j=1; j<n+1; ++j)//Traverse all Nodes to { + if(indegree[j]==0)//Find a node with an entry level of 0 - { theindegree[j]--;//decrease in degrees to avoid the next time you find * if(i!=N) $ {Panax Notoginsengcout<<j<<" "; - } the Else +cout<<j<<Endl; A for(k=1; k<n+1;++k) the { + if(map[j][k]==1)//Delete an edge associated with a node with a degree of 0 - { $indegree[k]--; $ } - } - Break; the } - }Wuyi } the } - //getch (); Wu return 0; -}
Hduoj 1285 determine the tournament position