Poj1094-sorting It all out-topology sequencing

Source: Internet
Author: User

Test Instructions:

1). Give you some capital letters, a total of n; there is m-bar relationship between capital letters;

2). Example: Relationship: a<b, meaning that A is in front of B (that is, maybe B is in front of a)

3). Output: There are three kinds of cases:

1.N letters under the relationship of the former I can determine the sort relationship;

2.N Letters An error occurred when executing to the article I command, i.e. the loop: a indirect or direct less than B and b indirectly or directly less than A;

The 3.M command cannot determine the N-letter ordering.

Ideas:

The intuitive feeling is the topological sort, it should be noted that each read into a command to sort the topology once.

If you don't know enough about topological sequencing, see this blog: Click to view

and the order of three cases is more important!!

conditions 1 and 2 can be judged at the same time, finally judging the situation 3!!

It means:

If the execution of an order to article I can be determined in case 1 or 2, then the direct output;

If the M command is finished and the alphabetical order is not determined, the output condition 3

Topic Links:

Click to do a question

AC Code:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<Set>#include<string>#include<stack>#include<cmath>#defineTest printf ("***\n")#defineMM1 (a) memset ((a), -1,sizeof ((a)))#defineMM0 (a) memset ((a), 0,sizeof ((a)))#defineMMX (a) memset ((a), 0x3f,sizeof ((a)))#defineKa GetChar (); GetChar ()#defineKA1 GetChar ()#defineIIS Std::ios::sync_with_stdio (FALSE)using namespacestd; typedefLong LongLL; typedef unsignedLong LongULL; Const intN = the; Const intM =4100005; Const intX =999983; Const intINF =1e9; Const DoubleEPS = 1e-8; Const intMoD = 1e9 +7; //#define DEBUGintn,m,sum; intTot,flag; int inch[N],head[n],tin[n]; intAr[n]; structLP {intU,v,nex; LP () {} LP (intAintBintc): U (A), V (b), NEX (c) {}}cw[n*N]; InlinevoidAddintAintb) {cw[++tot]=LP (A,b,head[a]); Head[a]=tot; }  intTuopu () {Queue<int>Q;  while(!Q.empty ())      Q.pop ();  for(intI=0; i<=n;++i) tin[i]=inch[i];  for(intI=1; i<=n;++i) {          if(tin[i]==0) {Q.push (i); }      }      intt=0; intans=0;  while(!Q.empty ()) {          if(Q.size () >1) ans=-1; intu=Q.front ();          Q.pop (); Ar[t++]=u;  for(inti=head[u];i!=-1; i=Cw[i].nex) {              intv=cw[i].v; TIN[V]--; if(tin[v]==0) {Q.push (v); }          }      }      if(t!=n)return 1; returnans; } Inlinevoidinit () {tot=-1; MM0 (inch);  MM1 (head); }  intMainintargcChar Const*argv[]) {#ifdef DEBUG freopen ("d:in.in","R", stdin); Freopen ("D:out.out","W", stdout); #endif      Charb;  while(~SCANF ("%d%d", &n,&m) && (n+m))          {init (); intans=1;  for(intI=0; i<m;++i) {              Chars[5]; scanf ("%s", s); A=s[0];b=s[2]; if(ans==0)Continue; inch[B-'A'+1]++; Add (a-'A'+1, B-'A'+1); intok=Tuopu (); if(ok==1) {ans=0; printf ("inconsistency found after%d relations.\n", i+1); }Else if(ok==0) {ans=0; printf ("Sorted sequence determined after%d relations:", i+1);  for(intI=0; i<n;++i) {printf ("%c", ar[i]+'A'-1); } printf (". \ n"); }          }          if(ans) {printf ("Sorted sequence cannot be determined.\n"); }      }      return 0; }  
View Code

Poj1094-sorting It all out-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.