Description
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
The input has several groups, the first behavior in each group is two number n (1<=n<=500), M, where n represents the number of troops, and m indicates 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
Give 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
#include <stdio.h>#include<string.h>intmp[510][510];intn,indegree[510];voidToposort () {inti,j,top,k=0; for(i=0; i<n;i++)///Traverse this adjacency matrix { for(j=1; j<=n;j++) { if(indegree[j]==0)///Find a node with an entry level of 0{Top=J; Break; } } if(i==n-1) {printf ("%d\n", top); } Else{printf ("%d", top); } Indegree[top]--;///Update the node's entry to-1 to avoid repeating the queue for(j=1; j<=n;j++)///deletes the edge associated with the point (that is, the entry of points connected to that point minus one) { if(Mp[top][j]) {Indegree[j]--; } } }}intMain () {inti,a,b,m; while(SCANF ("%d%d", &n,&m)! =EOF) {memset (Indegree,0,sizeof(Indegree));///initialized to 0Memset (MP,0,sizeof(MP)); for(i=0; i<m;i++) {scanf ("%d%d",&a,&b); if(mp[a][b]==0) {Mp[a][b]=1;///The two teams started to establish contact .indegree[b]++;///the former prevails over the latter, and the latter's penetration adds a}} toposort ();///Start Topology }}
Determine the tournament position (basic example of topological sorting)