POJ 1094 Sorting It All Out Floyd_Washall+Topological_sort

~hsm~發表於2019-02-25

title

POJ 1094

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 40141 Accepted: 14117

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

code

#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
using namespace std;
const int maxn=27;
template<typename T>inline void read(T &x)
{
	x=0;
	T f=1,ch=getchar();
	while (!isdigit(ch) && ch^'-') ch=getchar();
	if (ch=='-') f=-1, ch=getchar();
	while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
	x*=f;
}
int n,m;
int d[maxn][maxn];
inline int Floyd_Washall(int n)
{
	for (int k=1;k<=n;++k)
		for (int i=1;i<=n;++i)
			for (int j=1;j<=n;++j)
				d[i][j]|=d[i][k]&d[k][j];
	for (int i=1;i<=n;++i)
		if (d[i][i]==1)
			return 1;
	return 0;
}
int ans[maxn],deg[maxn],vis[maxn];
inline int Topological_sort(int n)
{
	int x=0,cnt;
	memset(deg,0,sizeof(deg));
	memset(ans,0,sizeof(ans));
	memset(vis,0,sizeof(vis));
	for (int i=1;i<=n;++i)
		for (int j=1;j<=n;++j)
			if (d[i][j]==1)
				++deg[j];
	for (int i=1;i<=n;++i)
	{
		cnt=0;
		for (int j=1;j<=n;++j)
			if (!deg[j] && !vis[j])
				++cnt,x=j;
		if (cnt>=2)	return 0;
		else if (!cnt)	return 1;
		ans[i]=x;
		vis[x]=1;
		for (int j=1;j<=n;++j)
			--deg[j];
	}
	return 2;
}
int flag;
char ch[410][5];
int main()
{
	while (1)
	{
		read(n);read(m);
		if (!n && !m) break;
		memset(d,0,sizeof(d));
		flag=0;
		for (int i=1;i<=m;++i)
			scanf("%s",ch[i]);
		int k;
		for (k=1;k<=m;++k)
		{
			int a=ch[k][0]-'A'+1;
			int b=ch[k][2]-'A'+1;
			d[a][b]=1;
			flag=Floyd_Washall(n);
			if (flag==1) break;
			flag=Topological_sort(n);
			if (flag==2) break;
		}
		if (flag==1)
			printf("Inconsistency found after %d relations.\n",k);
		else if (!flag)
			printf("Sorted sequence cannot be determined.\n");
		else
		{
			printf("Sorted sequence determined after %d relations: ",k);
			for (int i=1;i<=n;++i)
				printf("%c",ans[i]+'A'-1);
			printf(".\n");
		}
	}
	return 0;
}

相關文章