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.
题目大意
给出由n个变量(2<=n<=26,变量名为一个大写字母,不同的变量字母不同,保证变量名是从‘A’开始依次选取的n个)组成的m个不等式(形如A<B),要求判断这一系列不等式能不能确定所有变量的大小关系;若能,输出在第几个不等式时可以得出结论,并输出最终排好序的变量名;若有不确定的大小关系,输出“Sorted sequence cannot be determined.”,若不能,输出在第几个不等式时冲突。
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.
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.
题解
思想是:依次加入每一个条件,用拓扑排序检验是否有环即可。只是需要在拓扑排序时注意一些特殊的情况……否则wa得飞起……
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,in[30],pd[30],map[30][30];
int ans[30],top;
char a[10];
int toposort()
{
int i,t[30],ct,j,tag=1,loc;
top=0;
for(i=1;i<=n;i++) t[i]=in[i];
for(i=1;i<=n;i++)
{ct=0;
for(j=1;j<=n;j++)
{if(t[j]==0)
{ct++; loc=j;}
}
if(ct==0) return 0;
if(ct>1) tag=-1;
ans[++top]=loc;
t[loc]=-1;
for(j=1;j<=n;j++)
{if(map[loc][j]==1) t[j]--;}
}
return tag;
}
void work()
{
int i,j,x,y,s,tag=0;
memset(map,0,sizeof(map));
memset(in,0,sizeof(in));
for(i=1;i<=m;i++)
{scanf("%s",a);
if(tag) continue;
x=a[0]-'A'+1; y=a[2]-'A'+1;
map[x][y]=1; in[y]++;
s=toposort();
if(s==0)
{printf("Inconsistency found after %d relations.\n",i);
tag=1;
}
else if(s==1)
{printf("Sorted sequence determined after %d relations: ",i);
for(j=1;j<=top;j++) printf("%c",ans[j]-1+'A');
puts(".");
tag=1;
}
}
if(!tag) puts("Sorted sequence cannot be determined.");
}
int main()
{
while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)
work();
return 0;
}