[Cpp]/* AOV network and topological sorting 1. In a directed acyclic graph, vertices are used to represent activity, and directed edges <u, v> are used to represent activity u first and activity v, this directed graph is called the AOV network. 2. If <u, v>, u is the direct precursor of v, and v is the direct successor of u. If <u, u1, u2, · un, v> u is the precursor of v, and v is the successor of u. Www.2cto. com3. There are transmission and anti-Self-inversion relationships in the precursor Successor Relationship. It can be inferred that the AOV network must be a directed acyclic graph. 4. Implementation of topological sorting: 1) Select a vertex with an inbound degree of 0 from the AOV network and output it; 2) delete the vertex from the AOV network and all edges sent from the vertex; 3) Repeat 1) and 2) until the vertex with an inbound degree of 0 is not found; 4) if all vertices are output, the topology is sorted. Otherwise, the graph has a ring chart. Input Description: The number of vertices n and number of edges m, and the number of m rows is the starting point u and v of each directed edge. The vertex sequence number starts from 1, the last line of the input file is 0 0, indicating the end. Input example: sample output: 6 8 5 1 4 3 2 6 1 2 Network has a cycle! 1 4 2 6 3 2 6 5 1 5 2 5 6 6 6 8 1 1 2 2 2 5 3 4 4 4 6 5 5 6 0 0 0 */# include <stdio. h> # include <string. h> # define MAXN 10 struct Arcnode {int to; struct Arcnode * next;}; Arcnode * List [MAXN]; int n, m, count [MAXN]; char output [100]; void topsort () {int I, top =-1; Arcnode * temp; bool bcycle = false; int pos = 0; for (I = 0; I <n; I ++) if (count [I] = 0) {count [I] = top; top = I ;}for (I = 0; I <n; I ++) {if (top =- 1) {bcycle = true; break;} else {int j = top; top = count [top]; pos ++ = sprintf (output + pos, "% d ", j + 1); temp = List [j]; while (temp! = NULL) {int k = temp-> to; if (-- count [k] = 0) {count [k] = top; top = k ;} temp = temp-> next ;}}if (bcycle) printf ("Network has a cycle! \ N "); else {int len = strlen (output); output [len-1] = 0; printf (" % s \ n ", output) ;}} int main () {int I, v, u; // freopen ("input.txt", "r", stdin); while (scanf ("% d", & n, & m )! = EOF) {www.2cto.com if (n = 0 & m = 0) break; memset (List, 0, sizeof (List); memset (count, 0, sizeof (count); memset (output, 0, sizeof (output); Arcnode * temp; for (I = 0; I <m; I ++) {scanf ("% d", & u, & v); u --; v --; count [v] ++; temp = new Arcnode; temp-> to = v; temp-> next = NULL; if (List [u] = NULL) List [u] = temp; else {temp-> next = List [u]; List [u] = temp ;}} topsort (); for (I = 0; I <n; I ++) {temp = List [I]; while (temp! = NULL) {List [I] = temp-> next; delete temp; temp = List [I] ;}} return 0 ;}