Sorting It All Out topological sort && determines whether there is a ring, if the relationship between all the points comprising

Problem Description
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
 

 

Input
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will 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, output consists of one line. This line should be 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 is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. 
 

 

Sample Input
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0
 

 

Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
************************************************** ************************************************** ********************** classic topological sorting
***************************************************************************************************************************
  . 1 #include <the iostream>
   2 #include < String >
   . 3 #include <CString>
   . 4 #include <cstdio>
   . 5 #include <Queue>
   . 6  the using  namespace STD;
   . 7  int A [ 26 is ] [ 26 is ];
   . 8 Queue < char > Ql; // record the relationship between the two 
  . 9  int de [ 26 is ], DE2 [ 26 is ];
 10  int relations;
 . 11  BOOL in Flag;
 12 is  int kind, IT, JT, KT, n-, m;
 13 is void carry(int n,int ret)
 14 {
 15   int i,j,sum=0;
 16   int num;
 17   bool sign=true;
 18   queue<int>Q;
 19   for(i=0;i<n;i++)
 20   {
 21       if(de[i]==0)
 22         Q.push(i);
 23   }
 24   while(!Q.empty())
 25   {
 26       NUM = 0 ;
 27       for (I = 0 ; I <n-; I ++ )
 28       {
 29           IF (de [I] == 0 )
 30             NUM ++ ;
 31 is       }
 32       IF (NUM> . 1 )
 33 is         Sign = to false ; // present uncertainty relation 
34 is       I = Q.front ();
 35       Q.pop ();
 36       Q1.push (I + ' A ' ); //
 37 [       SUM ++ ;
 38 is      de [I] = - . 1 ;
 39       for (J = 0 ; J <n-; J ++ )
 40       {
 41 is           IF (A [I] [J] == to true )
 42 is           {
 43 is               de [J] - ;
 44 is               IF ( de [J] == 0 )
 45                Q.push (J);
 46 is           }
 47       }
 48  
49  
50    }
 51 is    // if there is determined the relationship between recording position, no count down 
52 is    IF (SUM n-== && Sign)
 53 is    {
 54       = In Flag to true ;
 55        kind = . 1 ;
 56 is        Relations = RET;
 57 is    }
 58    the else 
59       for (I = 0 ; I <n-; I ++ )
 60         IF (de [I]> 0 ) // if the presence of a ring, not to the operator 
61 is          {
 62 is             In Flag = to true ;
 63 is             kind = 2 ;
 64             Relations = RET;
 65             BREAK ;
 66          }
 67      IF(! In Flag) // If not satisfied, the recovery 
68      {
 69          for (I = 0 ; I <n-; I ++ )
 70            de [I] = DE2 [I];
 71 is      }
 72      return ;
 73 is  
74  }
 75  int main ( )
 76  {
 77      char left, MID, right;
 78      int Ni, NJ;
 79      the while (Scanf ( " % D% D " , & n-, & m))
 80      {
 81          getchar ();
 82          In Flag =false;
 83         kind=0;
 84         relations=0;
 85         if(n==0&&m==0)
 86          break;
 87         for(it=0;it<n;it++)//初始化
 88         {
 89             for(jt=0;jt<n;jt++)
 90               a[it][jt]=false;
 91             de[it]=-1;
 92             de2[it]=-1;
 93         }
 94          for (IT = 0 ; IT <m; IT ++ )
 95          {
 96              CIN MID >> >> >> left right;
 97              getchar ();
 98              Ni = LEFT - ' A ' ; // when the two are linked, first of all set into 0 
99              NJ right- = ' A ' ;
 100              IF (de [Ni] == - . 1 )
 101              {
 102                  de [Ni] = 0 ;
 103                  DE2 [Ni] = 0 ;
 104             }
 105              an if (de [nj] == - 1 )
 106              {
 107                  de [nj] = 0 ;
 108                  de2 [nj] = 0 ;
 109              }
 110              an if (! A [ni] [nj])
 111              {
 112                  a [ Ni] [nj] = True ; // Ni sum nj Yes relations 
113                  De [nj] Tasutasu ;
 114                  De2 [nj] Tasutasu ;
 115              }
 116              If (! Flag)
 117             {
 1 18                 the while (! Q1.empty ()) // is not found determine the relationship, has been restored to 
119                   Q1.pop ();
 120                 with Carry (n-, IT + . 1 );
 121              }
 122          }
 123          IF (In Flag)
 124          {
 125              IF (== kind . 1 )
 126              {
 127                  the printf ( " the Sorted Sequence Determined After Relations% D: " , Relations);
 128                  the while (! Q1.empty ())
 129                  {
130                     cout<<Q1.front();
131                     Q1.pop();
132                 }
133                 cout<<"."<<endl;
134             }
135             else
136                 if(kind==2)
137                 {
138                   printf("Inconsistency found after %d relations.\n",relations);
139                 }
140         }
141         else
142             printf("Sorted sequence cannot be determined.\n");
143 
144     }
145 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3389815.html

Guess you like

Origin blog.csdn.net/weixin_33994429/article/details/93432848