# 【POJ】1094 Sorting It All Out 拓扑排序

Sorting It All Out
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26683 Accepted: 9220

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

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define clear( a , x ) memset ( a , x , sizeof a )
#define copy( a , b ) memcpy ( a , b , sizeof a )

typedef long long Int ;

const int MAXN = 27 ;
const int MAXE = 1000000 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
int v , n ;
} ;

Edge edge[MAXE] ;
int in[MAXN] , rin[MAXN] ;
int d[MAXN] ;
int Q[MAXN] , head , tail ;
int G[MAXN][MAXN] ;
int n , m ;
int tot ;
int vis[MAXN] ;
int ans[MAXN] ;

void addedge ( int u , int v ) {
edge[cntE].v = v ; edge[cntE].n = adj[u] ; adj[u] = cntE ++ ;
}

int DAG ( int idx ) {
head = tail = 0 ;
clear ( d , 0 ) ;
copy ( in , rin ) ;
int num = 0 ;
REP ( i , n )
if ( !in[i] )
Q[tail ++] = i , d[i] = 1 ;
while ( head != tail ) {
int u = Q[head ++] ;
ans[num ++] = u ;
for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
int v = edge[i].v ;
if ( d[v] < d[u] + 1 )
d[v] = d[u] + 1 ;
if ( 0 == ( -- in[v] ) )
Q[tail ++] = v ;
}
}
if ( num < tot ) {
printf ( "Inconsistency found after %d relations.\n" , idx ) ;
return 1 ;//cycle
}
int cnt = 0 ;
REP ( i , n )
if ( d[i] > cnt )
cnt = d[i] ;
if ( cnt == n ) {
printf ( "Sorted sequence determined after %d relations: " , idx ) ;
REP ( i , num )
printf ( "%c" , ans[i] + 'A' ) ;
printf ( ".\n" ) ;
return 1 ;//ok
}
else
}

void work () {
int u , v ;
char s[5] ;
while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) {
clear ( adj , -1 ) ;
clear ( rin , 0 ) ;
clear ( vis , 0 ) ;
cntE = 0 ;
tot = 0 ;
int ok = 0 ;
REPF ( i , 1 , m ) {
scanf ( "%s" , s ) ;
if ( ok ) continue ;
u = s[0] - 'A' ;
v = s[2] - 'A' ;
if ( !vis[u] )
tot ++ , vis[u] = 1 ;
if ( !vis[v] )
tot ++ , vis[v] = 1 ;
++ rin[v] ;
addedge ( u , v ) ;
ok = DAG ( i ) ;
}
if ( !ok )
printf ( "Sorted sequence cannot be determined.\n" ) ;
}
}

int main () {
work () ;
return 0 ;
}


• 本文已收录于以下专栏：

## POJ-1094 Sorting It All Out 解题报告

Description An ascending sorted sequence of distinct values is one in which some form of a less-t...
• angle555945
• 2012年03月24日 01:03
• 891

## POJ1094-Sorting It All Out

• lyy289065406
• 2011年07月30日 09:25
• 7192

## 【POJ 1094】Sorting It All Out

【POJ 1094】Sorting It All Out
• ChallengerRumble
• 2015年07月05日 23:16
• 519

## 拓扑排序Sorting It All Out

zoj1060 poj1094 Sorting It All Out 题意：
• u011699990
• 2014年08月06日 20:36
• 565

## POJ1094 字母排序（拓扑排序）

• alongela
• 2012年12月05日 16:28
• 3465

## POJ 1094 Sorting It All Out （拓扑排序）

• u013961718
• 2015年10月26日 22:38
• 220

## [POJ 1094]Sorting It All Out[拓扑排序]

• CatGlory
• 2016年01月20日 00:53
• 298

## POJ 1094 Sorting It All Out(拓扑排序变形)

Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30293   Accept...
• helloiamclh
• 2015年08月26日 15:56
• 259

## POJ 1094 Sorting It All Out 拓扑排序

• wuxuanyi27
• 2016年06月18日 11:19
• 163

## Poj 1094 Sorting It All Out (传递闭包+拓扑排序)

• whyorwhnt
• 2013年09月10日 20:40
• 1590

举报原因： 您举报文章：【POJ】1094 Sorting It All Out 拓扑排序 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)