Test instructions
Given N, which represents the N letters starting with a, gives their m-less relation (A<B).
If the first I relationship can be determined in one order of n letters output:
Sorted sequence determined after I relations: lined letters.
If the first I relationship begins to cause a contradiction, the output is:
Inconsistency found after I relations.
M relationships can not be determined in the order of output:
Sorted sequence cannot be determined.
Code
/*G[I][J] for the adjacency matrix, E[i] for the entry of I in==1 represents a contradiction, the d==1 stands for determining the order so in order */#include <cstdio> #include <cstring> #define N 30int N, M, G[n][n], e[n], C[n], a, B, in, D, So[n];char va, vb;void topo () {memcpy (c, E, sizeof e);//copy E to C, because the following changes to C, the Can not be used directly with the e int i, top =-1; for (i = 0; i < n; i++) if (!c[i]) {C[i] = top;//simulates the subscript stack, the point of 0 is passed the subscript stack to top of C = i; } for (i = 0; i < n; i++) {if (top = =-1)//n cycles, if a point of 0 is not found at a time, it returns to the -1 {in = 1;//description In the ring return; } int u = top;//with top top = C[u]; So[i] = u; for (int v = 0; v < n; v++) if (G[u][v] && (--c[v]) = = 0)//The point at which the 0 is removed after you leave the edge { C[V] = top; Stack top = v; }} d = 1; for (i = 1; i < n; i++)//Determine if the order is deterministic if (!g[so[i-1]][so[i]]) d = 0;//If there is no partial order relationship between adjacent two, then it is an indeterminate order}int main () {/ /freopen ("In.txt", "R", stdin); while (~SCANF ("%d%d", &n, &m)&& N) {memset (g, 0, sizeof G); memset (e, 0, sizeof e); d = in = 0; for (int i = 1; I <= m; i++) {scanf ("%c<%c", &va, &VB); A = va-' a '; b = vb-' A '; if (!g[a][b] &&!d &&!in) {g[a][b] = 1; e[b]++; Topo (); if (in) printf ("Inconsistency found after%d relations.\n", i); else if (d) {printf ("Sorted sequence determined after%d relations:", I); for (int j = 0; J < N; j + +) printf ("%c", So[j] + ' A '); printf (". \ n"); }}} if (!d &&!in) printf ("Sorted sequence cannot be determined.\n"); }}
"POJ 2186" topology sequencing