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.
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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
const int ms=;
int n,m;
bool appear[ms];
char output[ms+];
int cnt[ms];
int tmp[ms];
vector<vector<char> > v;
int topo_sort(int s)
{
int i,j,k,flag=;
int total=,r=;
for(i=;i<n;i++)
tmp[i]=cnt[i];
while(s--)
{
total=;
for(i=;i<n;i++)
if(appear[i]&&tmp[i]==)
{
j=i;
total++;
}
if(total>=)
{
if(total>)
flag=;
for(i=;i<v[j].size();i++)
tmp[v[j][i]]--;
tmp[j]=-;
output[r++]=j+'A';
output[r]=;
}
else
return -;
}
if(flag)
return r;
return ;
}
int main()
{
int i,j,k,judge,det;
char str[];
while(scanf("%d%d",&n,&m)==&&(n+m))
{
judge=;
det=;
int sum=;
v.clear();v.resize(n);
memset(cnt,,sizeof(cnt));
memset(appear,false,sizeof(appear));
for(i=;i<=m;i++)
{
scanf("%s",str);
cnt[str[]-'A']++;
v[str[]-'A'].push_back(str[]-'A');
if(!appear[str[]-'A'])
{
sum++;
appear[str[]-'A']=;
}
if(!appear[str[]-'A'])
{
sum++;
appear[str[]-'A']=;
}
if(judge==)
{
det=topo_sort(sum);
if(det==-)
{
judge=-;k=i;
}
else if(det==n)
{
judge=;
k=i;
}
}
}
if(judge==-)
printf("Inconsistency found after %d relations.\n",k);
else if(judge==)
printf("Sorted sequence cannot be determined.\n");
else
printf("Sorted sequence determined after %d relations: %s.\n",k,output);
}
return ;
}