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.
题目大意:
给定n个元素和m对先后顺序,问用前几个条件可以对这n个元素进行以下其中一项的判定:
1、n个元素的顺序唯一确定。
2、元素的先后顺序出现矛盾(图中含环)。
如果全部的条件都无法给出判定,则按要求输出无法判定。
核心思想:
依次将条件逐个放进图中,每放入一个条件就进行一次拓扑排序,直到可以输出为止。
详见代码。
代码如下:
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int N=34;
vector<int>vec[N];//vec[i]存i的后继
char s[12];
int tru[N],ru[N],ans[N];//ru[i]和tru[i]存i的入度,ans[i]存顺序
int fun(int n)
{
queue<int>q;
for(int i=0;i<n;i++)
if(!ru[i])
q.push(i);
int flag=1,sum=0;//flag用于判定顺序是否唯一,sum用于判环
while(!q.empty())
{
int te=q.front();
ans[sum++]=te;
q.pop();
//若此时q非空,则在pop之前,q中元素数量>=2
//这些元素同时出现在q中,意味着这些元素先后顺序不定
if(!q.empty())
flag=0;//顺序不唯一
int len=vec[te].size();
//元素te删除,其所有后继的入度-1
for(int i=0;i<len;i++)
{
int t=vec[te][i];
ru[t]--;
if(!ru[t])
q.push(t);
}
}
if(sum<n)
return 2;//有环
if(flag)
return 1;//顺序完全确定
return 0;//其他
}
int main()
{
int n,m;
while(1)
{
scanf("%d%d",&n,&m);
if(!n&&!m)
break;
int flag=0;
//依次将条件逐个放进图中
for(int i=1;i<=m;i++)
{
scanf("%s",s);
if(flag)continue;
int x=s[0]-'A';
int y=s[2]-'A';
tru[y]++;
vec[x].push_back(y);
for(int i=0;i<n;i++)
ru[i]=tru[i];
int re=fun(n);
if(re)//已经有答案,可以输出了
{
flag=1;
if(re==1)//顺序唯一确定
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;j++)
printf("%c",ans[j]+'A');
printf(".\n");
}
else//有环
printf("Inconsistency found after %d relations.\n",i);
}
}
if(!flag)//一直无答案,则输出无法判定
printf("Sorted sequence cannot be determined.\n");
for(int i=0;i<n;i++)
{
vec[i].clear();
tru[i]=0;
}
}
return 0;
}