"POJ 2186" topology sequencing

Source: Internet
Author: User

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, &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.