Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
----------------------------------------------------------------------------------------------------------------------------------------------------------
Welcome to tianci Hut: http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
1. Output any one that meets the priority criteria:
The Code is as follows:
#include <cstdio>#include <cstring>#define MAXN 517int G[MAXN][MAXN];int c[MAXN],topo[MAXN];int t;int n, m;bool dfs(int u){c[u] = -1;for(int v = 1; v <= n; v++){if(G[u][v]){if(c[v] < 0)return false;else if(!c[v] && !dfs(v))return false;}}c[u] = 1;topo[--t] = u;return true;}bool toposort(){t = n;memset(c,0,sizeof(c));for(int u = 1; u <= n; u++){if(!c[u]){if(!dfs(u))return false;}}return true;}int main(){int a, b;int i;while(~scanf("%d%d",&n,&m)){for(i = 0; i < m; i++){scanf("%d%d",&a,&b);G[a][b] = 1;}if(toposort()){bool ok = true;for(i = 0; i < n; i++){if(ok){printf("%d",topo[i]);ok = false;}elseprintf(" %d",topo[i]);}}printf("\n");}return 0;}
2. When the output meets the conditions and requires that the output be numbered in front of the following:
In this case, priority queue is required;
Topology Sorting first counts the inbound degree of each node. Add the node number with the degree of 0
Queue(This question is placed in the priority queue. Then perform a loop:
- This node is used to retrieve the head node of the team and is regarded as the start point of the edge.
- Then, "Delete the edge connected to the vertex", the code is to subtract one of the inbound degrees of the other node (that is, the end point) of the edge in the graph;
- If the number of the end point is changed to 0 after the subtractionQueue.
- Determines whether the queue is empty. If it is not empty, return to 1
Class with priority queue in priority queue C ++ STL -- priority_queue <t>. The higher the default priority queue value, the higher the priority. For example, priority_queue <int> q. The elements are arranged in descending order. If we want to implement the ascending order, we need to reload it.
priority_queue<int,vector<int>,greater<int> > q;
The Code is as follows: (hdu1285)
#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); }}
3. Directly output the point with zero input each time: (hdu1285)
The Code is as follows:
# 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 ;}
The preceding sections describe how to use a two-dimensional graph to store an adjacent matrix. This graph is only applicable to a dense graph and cannot store as big as the data size;
The following is the topological sorting of the graph stored in the adjacent table. It is applicable to big data and sparse graphs!
I. 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 ;}
2. structure (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 505struct 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;}
Topology Sorting (template to be updated)