POJ 1094, Sorting It All Out

拓扑排序


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.

 

Source
East Central North America 2001

 

*check loop first, then ambiguous


 

//  POJ1094.cpp : Defines the entry point for the console application.
//

#include 
< iostream >
#include 
< string >
#include 
< algorithm >
using   namespace  std;

string  TopSort( bool  d[ 26 ][ 26 ],  int   in [ 26 ],  int  n)
{
    
int  indegree[ 26 ];
    copy (
& in [ 0 ],  & in [n],  & indegree[ 0 ]);

    
int  cnt  =  n;
    
string   str;
    
bool  ambiguous  =   false ;
    
while  (cnt  >   0 )
    {
        
int  zeros  =  std::count( & indegree[ 0 ], & indegree[n],  0 );
        
if  (zeros  ==   0 )
        {
            
return   " 1 " //  loop
        }
        
else   if  (zeros  >   1 )
        {
            ambiguous 
=   true // ambiguous
        }
        
        
int  pos  =  std::distance( & indegree[ 0 ],std::find( & indegree[ 0 ], & indegree[n],  0 ));

        
for  ( int  i  =   0 ; i  <  n;  ++ i)
            
if  (d[pos][i]  ==   true -- indegree[i];

        
-- cnt;
        indegree[pos] 
=   - 1 ;
        str 
+=   string ( 1 ,( char )(pos  +   ' A ' ));
    }

    
if  (ambiguous  ==   true ) return   " 2 " ;
    
return  str;  // OK
}

int  main( int  argc,  char *  argv[])
{
    
int  n,m;
    
int   in [ 26 ];
    
bool  d[ 26 ][ 26 ];
    
string  line;
    
while (cin  >>  n  >>  m  &&  n  !=   0   &&  m  !=   0 )
    {
        memset(
in 0 sizeof ( in ));
        memset(d, 
0 sizeof (d));
        
string  result  =   "" ;
        
int  step  =   0 ;
        
for  ( int  i  =   1 ; i  <=  m;  ++ i)
        {
            cin 
>>  line;
            
if  (d[line[ 0 -   ' A ' ][line[ 2 -   ' A ' ] == false )
            {
                d[line[
0 -   ' A ' ][line[ 2 -   ' A ' =   true ;
                
++ in [line[ 2 -   ' A ' ];
                
if  (result  ==   "" || result  ==   " 2 " )
                {
                    result 
=  TopSort(d,  in , n); 
                    step 
=  i;
                }
            }
        }
        
        
if  (result  ==   " 1 " )
        {
            cout 
<<   " Inconsistency found after  " << step << "  relations.\n " ;
        }
        
else   if  (result  ==   " 2 " )
        {
            cout 
<<   " Sorted sequence cannot be determined.\n " ;
        }
        
else
        {
            cout 
<<   " Sorted sequence determined after  " << step << "  relations:  " << result << " .\n " ;
        }
    };
    
return   0 ;
}

你可能感兴趣的:(sort)