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

原创 2014年07月14日 10:44:30
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

传送门:【POJ】1094 Sorting It All Out

题目大意:给n个字母,以及字母间的关系,问能否找到确切的长度为n的字母排列(后面的大于前面的),输出有环或者不能确定或者存在。

题目分析:添加一条边就拓扑排序一次,写起来略烦。。。。不过还是1Y了。

代码如下:

#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 adj[MAXN] , cntE ;
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
		return 0 ;//not found
}

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
  • angle555945
  • 2012年03月24日 01:03
  • 891

POJ1094-Sorting It All Out

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299234850   提示:拓扑排序 这道题有隐含这一信息,每输入一对关系,如果判...
  • lyy289065406
  • lyy289065406
  • 2011年07月30日 09:25
  • 7192

【POJ 1094】Sorting It All Out

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

拓扑排序Sorting It All Out

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

POJ1094 字母排序(拓扑排序)

对于N个大写字母,给定它们的一些偏序关系,要求判断出经过多少个偏序关系之后可以确定它们的排序或者存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序。 利用拓扑排序即可解决这个问题,但由于题目...
  • alongela
  • alongela
  • 2012年12月05日 16:28
  • 3465

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

题目大意 给出一个数n,表示有n的大写字母,从A开始。然后给出m个字符串,均是X < Y的格式,其中X,Y均是单个的大写字母。 问根据这m个字符串得到前n个字母的关系:全序,有环,偏序 按以下三种格式...
  • u013961718
  • u013961718
  • 2015年10月26日 22:38
  • 220

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

题目链接:[POJ 1094]Sorting It All Out[拓扑排序] 题意分析: 给出字母表中的前N个大写字母和它们之间的M个比较关系,问:从哪句话起能够确定所有关系?或者从哪句话起出现...
  • CatGlory
  • 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
  • helloiamclh
  • 2015年08月26日 15:56
  • 259

POJ 1094 Sorting It All Out 拓扑排序

题目描述:DescriptionAn ascending sorted sequence of distinct values is one in which some form of a less-...
  • wuxuanyi27
  • wuxuanyi27
  • 2016年06月18日 11:19
  • 163

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

题意:给你一些大写字母间的大小排序关系,判断以下3中情况:1 能唯一确定它们的排列顺序,2 所给关系是矛盾的,3 到最后也不能确定它们之间的关系。 思路:出现矛盾也就是在比较大小关系的过程中出现环,...
  • whyorwhnt
  • whyorwhnt
  • 2013年09月10日 20:40
  • 1590
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【POJ】1094 Sorting It All Out 拓扑排序
举报原因:
原因补充:

(最多只允许输入30个字)