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
思路:拓扑排序,判断是否出现环,或者能排出序列,当出现多于一个入度为0的点则为不确定。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
const int mm=1100;
char out[mm];
bool vis[77];
vector<int >mp[77];
int cu[mm],cun[mm],n,m,pos;
bool yes;
int topsort()
{ for(int i=0;i<30;i++)
cu[i]=cun[i];
int top=-1,ttnum=0;
for(int i=0;i<n;i++)
if(cu[i]==0)
cu[i]=top,top=i;
if(cu[top]!=-1)yes=1;///只要入度为0的点多于1个那么就是不确定序列,但人需要判断是否有环
for(int i=0;i<n;i++)
{
if(top==-1)return 0;
else
{
int num=top;top=cu[top];
pos+=sprintf(out+pos,"%c",char('A'+num));
for(int j=0;j<mp[num].size();j++)
if(--cu[mp[num][j]]==0)
cu[mp[num][j]]=top,top=mp[num][j];
if(top>=0&&cu[top]!=-1)yes=1;
}
}return 1;
}
int main()
{
while(cin>>n>>m)
{ if(n==0&&m==0)break;
char sss,u,v;
memset(cun,0,sizeof(cun));
memset(mp,0,sizeof(mp));
memset(vis,0,sizeof(vis));
int ans=0,z=0,pp=0;
for(int i=0;i<m;i++)
{
cin>>u>>sss>>v;
cun[v-'A']++;mp[u-'A'].push_back(v-'A');
if(!vis[v-'A'])vis[v-'A']=1,pp++;
if(!vis[u-'A'])vis[u-'A']=1,pp++;///判断不同的字符数是否刚好为n个
if(ans==0)
{pos=0;yes=0;///yes是真说明拓扑排序的序列仍是不确定,但需要看看是否有环,无环则continue;
if(topsort()){if(yes)continue;if(pp==n)ans=1,z=i+1;}
else ans=-1,z=i+1;
}
}
if(ans==1)cout<<"Sorted sequence determined after "<<z<<" relations: "<<out<<".\n";
else if(ans==-1)cout<<"Inconsistency found after "<<z<<" relations.\n";
else cout<<"Sorted sequence cannot be determined.\n";
}
}