The simplest Topology Sorting method is as follows: (1) Select a vertex from the directed graph without a forward (that is, the inbound degree is 0) and output it. (2) Delete the vertex from the net and delete all directed edges from the vertex. (3) Repeat the preceding two steps until there are no longer any vertices without the precursor in the remaining network or all vertices are sorted. [cpp] # include <stdio. h> # include <string. h> # define N 505 int map [N] [N], index [N], used [N], n, m; // map indicates the adjacent matrix, index record inbound, used record sorting void toposort () {int I, j, topo = 0; while (topo <n) {for (I = 1; I <= n; I ++) {if (index [I] = 0 & used [I] = 0) {// I from small to large, if the number is small, used [I] = 1; break; // Delete the point where the input degree is 0} if (I = n + 1) Return; // ends if (topo) printf (""); printf ("% d", I); topo ++ if there is no entry point; for (j = 1; j <= n; j ++) {// delete all edges starting with I if (map [I] [j] = 1) {map [I] [j] = 0; index [j] -- ;}}} int main () {int I, a, B; while (scanf ("% d", & n, & m )! = EOF) {memset (map, 0, sizeof (map); memset (index, 0, sizeof (index); memset (used, 0, sizeof (used )); for (I = 1; I <= m; I ++) {scanf ("% d", & a, & B ); if (map [a] [B] = 0) {// when considering the duplicate edge, map [a] [B] = 1; index [B] ++ ;}} toposort (); printf ("\ n") ;}return 0 ;}