poj1094 (de-ring) (only OK) Topu sort

Source: Internet
Author: User

sorting It all Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 31940 Accepted: 11103

Description

An ascending sorted sequence of distinct values are one in which some form of a less-than operator are used to order the Ele ments from smallest to largest. For example, the sorted sequence A, B, C, D implies, a < B, b < C and C < D. In this problem, we'll give yo U a set of relations of the form a < B and ask you to determine whether a sorted order have been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing-positive integers n and m. The first value indicated the number of objects To sort, where 2 <= n <= 26. The objects to be sorted'll be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which'll be given in this problem instance. Next'll be M lines, each containing one such relation consisting of three Characters:an uppercase letter, the character "<" and a second uppercase letter. No letter would be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

Output

For each problem instance, the output consists of one line. This line should is one of the following three:

Sorted sequence determined after xxx relations:yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.

where xxx is the number of relations processed at the time either a sorted sequence are determined or an inconsistency is F Ound, whichever comes first, and yyy...y is the sorted, ascending sequence.

Sample Input

4 6a<ba<cb<cc<db<da<b3 2a<bb<a26 1a<z0 0

Sample Output

Sorted sequence determined after 4 RELATIONS:ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
A year ago, there was no idea, just according to other people use Floyd to judge the ring, now do the idea is to use Targan to judge the strong connected components (ring), after the elimination of the ring, then the topology to determine whether the first repair relationship
The only meaning is that a->b a->c c->d, then the relationship between B and C cannot be judged.
So every time you judge Indegree, just judge whether In==0 is the only
But the subject is a bit disgusting, test instructions asked the output can be judged when the number of relations, the expression is not clear,, so each step to use a topology to determine whether there is a single relationship, and then each step to do Targan to remove the ring, feel very troublesome appearance

Later looked at other people's code, found that directly with TOPU can judge there is no ring, the idea is very clear
We can easily find that every time topu judgment, as long as there is no in = = 0 of the node, then there must be a ring, there are multiple in==0 nodes, then this figure is not the only determined

Note: As long as the problem is found to be unique, or there is a contradiction (that is, the ring), it will be directly conditional, in other words, the problem is to use the smallest relationship to determine whether the only certainty, or contradiction
Special case is, the only certainty before, there are contradictions, according to the meaning of the question is no matter what, because the first to determine the unique
Very classic (excluding ring) (uniquely OK) topu sort
Rt
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intINF =0x3f3f3f;Const intmaxn= -;Const intMaxnn = 1e5+Ten;int inch[MAXN];intTMP[MAXN];intG[MAXN][MAXN];intANS[MAXN];intn,m,flag,signal;inttop;voidinit () {flag=1; Signal=0; memset (G,0,sizeof(G)); memset (TMP,0,sizeof(TMP)); memset (inch,0,sizeof(inch));}voidTuposort () {intsum,x; Top=0;  for(intI=0; i<n;i++) {Tmp[i]=inch[i]; }     for(intI=0; i<n;i++) {sum=0;  for(intj=0; j<n;j++){            if(tmp[j]==0) {sum++; X=J; }        }        if(sum==0) {flag= -1; return; }        if(sum>1) {flag=0; } Tmp[x]= -1; Ans[top++] =x;  for(intj=0; j<n;j++){            if(G[x][j]) {Tmp[j]--; }        }    }    //flag = 2;}intMain () {Charts[5]; inta,b,w;  while(SCANF ("%d%d",&n,&m), N) {init ();  for(intI=0; i<m;i++) {scanf ("%s", TS); A= ts[0]-'A'; b= ts[2]-'A'; if(!G[a][b]) {                inch[b]++; G[A][B]=1; }            Else{                Continue; }            if(signal)Continue; Flag=1;            Tuposort (); if(flag==-1|| flag==1) {W= i+1; Signal=1; }        }        if(flag==0) printf ("Sorted sequence cannot be determined.\n"); Else if(flag==-1) printf ("inconsistency found after%d relations.\n", W); Else{printf ("Sorted sequence determined after%d relations:", W);  for(intI=0; i<top;i++) {printf ("%c", ans[i]+'A'); } cout<<"."<<Endl; }    }}
View Code

poj1094 (de-ring) (only OK) Topu sort

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.