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

                                           Sorting It All Out

Time Limit: 1000MS   Memory Limit: 10000K

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.

题意:给出m个比较式,问能否能确定有序数列,若不能,是条件不足还是因为有环存在。

解题思路:  拓扑排序,(一个字母看成一个点) 没输入一个式子进行一次拓扑排序,若同时有多个入度为0的点,说明条件不足,若没有入度为0的点后,确定顺序的点少于n个,说明有环存在。要先判断是否有环再判断是否能确定顺序,前者是后者的真子集

ACCode:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;
int n,_in[27],tmp[27],ans[27];
vector< int> _out[27];
bool vis[27][27],flag;
void init()
{
	for(int i=0;i<n;i++) {
		_in[i] = 0;
		_out[i].clear();
	}
	memset(vis,false,sizeof vis);
	flag = false;
}
int Topo()
{
	queue< int> q;
	int len=0;
	for(int i=0;i<n;i++) {
		if(_in[i] == 0) 
			q.push(i);
	}
	bool uflag = false;
	while(!q.empty()) {
		if(q.size() > 1) uflag=true;; //还不能判断
		int t = q.front();
		q.pop();
		ans[++len] = t;
		for(int i=0;i<_out[t].size();i++) {
			_in[_out[t][i]]--;
			if(_in[_out[t][i]] == 0)
				q.push(_out[t][i]);
		}
	}
//  要先判断是否有环再判断是否能确定顺序,前者是后者的真子集
	if(len < n) return 1; 
	if(uflag) return 2; 
	return 3;

}
int main()
{
	int m,t;
	char ord[4];
	while(~scanf("%d%d%*c",&n,&m) && (n || m)) {
		init();
		for(int i=1;i<=m;i++) {
			scanf("%s",ord);
			if(flag) continue;
			if(ord[1] == '<' && !vis[ord[0]-'A'][ord[2]-'A']) {
				vis[ord[0]-'A'][ord[2]-'A'] = true;
				_in[ord[2]-'A']++;
				_out[ord[0]-'A'].push_back(ord[2]-'A');
			}
			else if(ord[1] == '>' && !vis[ord[2]-'A'][ord[0]-'A']) {
				vis[ord[2]-'A'][ord[0]-'A'] = true;
				_in[ord[0]-'A']++;
				_out[ord[2]-'A'].push_back(ord[0]-'A');
			}
			memcpy(tmp,_in,sizeof(_in));
			int t = Topo();
			memcpy(_in,tmp,sizeof(tmp));
			if(t == 1) {
				printf("Inconsistency found after %d relations.\n",i);
				flag = true;
			}
			else if(t == 3) {
				printf("Sorted sequence determined after %d relations: ",i);
				for(int i=1;i<=n;i++)
					printf("%c",char(ans[i]+'A'));
				printf(".\n");
				flag = true;
			}
		}
		if(!flag)
			printf("Sorted sequence cannot be determined.\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42765557/article/details/98223852