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个大写字母,给定它们之间的关系,要求判断下列三种情况
1.到第k次就能确定有环
2.到第k次就能确定字母间关系
3.直到判断完所有输入的字母关系,还是无法确定字母间关系
思路:每次输入都要进行拓扑排序来判断上述三种情况
AC代码:
//采用临接表存储字母关系
/*三种情况
1.到第k次就能确定有环
2.到第k次就能确定字母间关系
3.直到所以字母关系输尽,还是无法确定字母间关系
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N=30;
vector<int> G[N];
int n,m;
int deg[N],vis[N];
int d[N],ans[N];
//拓扑排序模板
int Tsort()
{
for(int i=0;i<n;i++)//新开一个数组存当前度的情况,如果用原来的deg[]则会改变度的状态
d[i]=deg[i];
queue<int> q;
for(int i=0;i<n;i++)
if(!d[i]) q.push(i);
int cnt=0,flag=0;
while(!q.empty())
{
if(q.size()>1) flag=1;//如果度为0的字母个数不为1,则是说明n个字母的关系处于不确定状态(如果能确定字母顺序,q.size()==1)
int p=q.front();
q.pop();
ans[cnt++]=p;//cnt统计度为0的字母个数,用于判断是否有环,如果cnt!=n,则说明有环
for(int i=0;i<G[p].size();i++)
{
int v=G[p][i];
d[v]--;
if(d[v]==0)
q.push(v);
}
}
if(cnt!=n) return -1;
else if(flag) return 0;
else return 1;
}
int main()
{
while(cin>>n>>m)
{
if(n==0&&m==0) break;
for(int i=0;i<n;i++)
G[i].clear();
memset(vis,0,sizeof(vis));
memset(deg,0,sizeof(deg));
char c[5];
int a,b,r,id,flag=0;
for(int i=1;i<=m;i++)
{
scanf("%s",c);
a=c[0]-'A';
b=c[2]-'A';
G[a].push_back(b);
deg[b]++;
if(flag) continue;//flag==1,说明字母顺序确定或者有环
r=Tsort();
if(r==1||r==-1)
{
flag=1;
id=i;
}
}
if(r==1)//到第k次就能确定字母间关系
{
printf("Sorted sequence determined after %d relations: ",id);
for(int i=0;i<n;i++)
printf("%c",ans[i]+'A');
printf(".\n");
flag=1;
}
else if(r==-1)//到第k次就能确定有环
{
printf("Inconsistency found after %d relations.\n",id);
flag=1;
}
else if(r==0)//直到所以字母关系输尽,还是无法确定字母间关系
{
printf("Sorted sequence cannot be determined.\n");
}
}
return 0;
}