Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 33760 Accepted: 11808
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
/*
题意:给你前n个大写字母之间的关系,问能否判断出他们之间的大小关系?
当输入一个个读入时,产生矛盾就输出,确定了关系也输出(不管后面有没有矛盾)。
若所有的全部处理后还无法判断,就输出无法判断
**思路:**
就是一个典型的top序列的问题。
处理矛盾:当存在环时就从产生了矛盾。判环分两种:(1)存在存在A->B,B->A这种,(2)A->B,B->C,C->A这种。
第一种直接判断,第二种当存在0个入度为0的顶点时就产生了。
处理第二种矛盾时,由于在judge函数中判断的,所以当入度为0的顶点大于1时,记录无法确定关系,并去掉其中一个入度为0的顶点。(可能矛盾还没有暴露出来)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 30;
int n,m,ans;
int out[maxn];
int d[maxn];
int e[maxn][maxn];
bool f1,f2;
bool judge()
{
bool vis[maxn];
bool tt=false;
memset(vis,0,sizeof(vis));
int t[maxn][maxn];
int dt[maxn];
for(int i=0;i<n;i++)
dt[i] = d[i];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
t[i][j] = e[i][j];
int num,tmp;
for(int i=0;i<n;i++)
{
num=0;
for(int j=0;j<n;j++)
{
if(!vis[j] && dt[j]==0)
{
num++;//入度为0数
tmp=j;
}
}
if(num==0)//A->B,B->C,C->A这种
{
printf("Inconsistency found after %d relations.\n",ans);
f1 = true;
return false;
}
if(num!=1) tt = true;//记录失败
vis[tmp] = true;
out[i] = tmp;
for(int k=0;k<n;k++)
{
if(t[tmp][k]==1) dt[k]--;
}
}
if(tt) return false;
for(int i=0;i<n;i++)
if(!vis[i]) return false;
return true;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
char s[5];
if(n==0 && m==0) break;
memset(e,0,sizeof(e));
memset(d,0,sizeof(d));
ans = 0;
f1=false, f2=false;
for(int i=0;i<m;i++)
{
scanf("%s",s);
if(f1 || f2) continue;
if(e[s[2]-'A'][s[0]-'A']==1)//存在存在A->B,B->A
{
printf("Inconsistency found after %d relations.\n",ans+1);
f1 = true; continue;
}
e[s[0]-'A'][s[2]-'A']=1;
d[s[2]-'A']++;
if(f2) continue;
ans++;
f2=judge();
if(f2)
{
printf("Sorted sequence determined after %d relations: ",ans);
for(int i=0;i<n;i++)
printf("%c",65+out[i]);
printf(".\n");
continue;
}
}
if(!f1 && !f2) printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
另外,可以直接使用floyd判环来判断矛盾
int Floyd() //判断是否有环。
{
int i,k,j;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
for(k=0; k<n; k++)
if( map[i][k] && map[k][j] )
map[i][j] = 1;
for(i=0; i<n; i++)
if( map[i][i] == 1 )
return 1;
return 0;
}