Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1285
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 Input
4 31 22 34 3
Sample output
1 2 4 3
The Code is as follows:
I. (direct method)
# Include <cstdio> # include <cstring> # define maxn 517int G [maxn] [maxn]; // path int in_degree [maxn]; // inbound int ans [maxn]; int n, m, X, Y; int I, j; void toposort () {for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {If (G [I] [J]) {in_degree [J] ++ ;}}for (I = 1; I <= N; I ++) // start from the smallest, {// This ensures that int K = 1 is output first when there are multiple answers with a smaller serial number; while (in_degree [k]! = 0) // find the point K ++ with zero input; ans [I] = K; in_degree [k] =-1; // update to-1, subsequent detection is not affected, which is equivalent to deleting a node for (Int J = 1; j <= N; j ++) {If (G [k] [J]) in_degree [J] --; // associated inbound subtraction 1 }}} void Init () {memset (in_degree, 0, sizeof (in_degree); memset (ANS, 0, sizeof (ANS); memset (G, 0, sizeof (g);} int main () {While (~ Scanf ("% d", & N, & M) {Init (); for (I = 0; I <m; I ++) {scanf ("% d", & X, & Y); G [x] [Y] = 1;} toposort (); for (I = 1; I <n; I ++) printf ("% d", ANS [I]); printf ("% d \ n", ANS [N]);} return 0 ;}
2. Topology Sorting + priority queue:
#include<iostream>#include<queue>#include<cstdio>#include<cstring>using namespace std;bool map[517][517];int in[517];priority_queue<int,vector<int>,greater<int> > q;void topo(int n){ for(int i=1;i<=n;i++) { if(in[i]==0) q.push(i); } int c=1; while(!q.empty()) { int v=q.top(); q.pop(); if(c!=n) { cout<<v<<" "; c++; } else cout<<v<<endl; for(int i=1;i<=n;i++) { if(!map[v][i]) continue; in[i]--; if(!in[i]) q.push(i); } }}int main(){ int n,m,i,j; while(cin>>n>>m) { int k=0; memset(map,0,sizeof map); memset(in,0,sizeof in); while(m--) { cin>>i>>j; if(map[i][j]) continue; map[i][j]=1; in[j]++; } topo(n); }}
Iii. Adjacent table + topological sorting:
①: Array-based adjacent table:
The Code is as follows:
# Include <iostream> using namespace STD; int ind [517]; // Number of indegree incoming degrees int [250017]; // adjacency list the position value of the adjacent table int adj_next [250017]; // The next pointer of the adjacent table int tail [517]; // The Last vertex of the adjacent table int main () {int N, m, I, j, a, B; while (scanf ("% d", & N, & M )! = EOF) {for (I = 0; I <= N; I ++) {tail [I] =-1; adj [I] =-1; adj_next [I] =-1; ind [I] = 0 ;}for (I = 0; I <m; ++ I) {scanf ("% d ", & A, & B); int x = tail [a], flag = 0; while (X! =-1) // determine whether to duplicate the edge {If (adj [x] = B) {flag = 1; break;} X = adj_next [X];} If (! Flag) // associate the adjacent table {adj [I] = B; adj_next [I] = tail [a]; tail [a] = I; IND [B] ++ ;}}for (I = 1; I <= N; I ++) // find n times {for (j = 1; j <= N; j ++) // traverse {If (IND [J] = 0) // when the inbound degree is 0, description: Previous {Ind [J] =-1; // skip if (I = 1) printf ("% d", j) when the next search inbound is 0 ); else printf ("% d", J); For (int K = tail [J]; k! =-1; k = adj_next [k]) // The inbound degree of the adjacent position minus one {Ind [adj [k] --;} break ;}}} printf ("\ n");} return 0 ;}
②: Struct (linked list) type list:
The Code is as follows:
#include<iostream>#include<cstring>#include<cstdlib>#include<cmath>#include<cstdio>#include<queue>#include<bitset>using namespace std;#define maxn 517struct node{ int num; node *next;};node map[maxn];int d[maxn], n;void Insert(int a, int b);void Free();void Topsort();int main(){ int m; while(cin >> n >> m) { int i, a, b; memset(d, 0, sizeof(d)); for(i=0; i<m; i++) { cin >> a >> b; Insert(a, b); d[b]++; } Topsort(); Free(); } return 0;}void Insert(int a, int b){ node *newnode; newnode = (node *)malloc(sizeof(node)); newnode->num = b; newnode->next = map[a].next; map[a].next = newnode;}void Free(){ node *cur, *old; for(int i=1; i<=n; i++) { cur = map[i].next; while(cur) { old = cur; cur = cur->next; free(old); } map[i].next = NULL; }}void Topsort(){ priority_queue<int, vector<int>, greater<int> > que; int nx, i; int q[maxn]={0}, k=0; node *cur; for(i=1; i<=n; i++) { if(d[i] == 0) que.push(i); } while(que.size()) { nx = que.top(), que.pop(); q[k++] = nx; cur = map[nx].next; while(cur) { nx = cur->num; d[nx] -= 1; if(d[nx] == 0) que.push(nx); cur = cur->next; } } cout << q[0]; for(i=1; i<k; i++) cout <<" "<< q[i]; cout <<endl;}
Hdu1285 determines the ranking of the competition (multiple methods of topological sorting)