Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32797 | Accepted: 11390 |
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.
Source
East Central North America 2001
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
char str[3];//记录边
int map[100][100];//储存字母之间的关系
int du[100];//记录入度数
int du2[100];//记录临时入度数
char a[100];//记录字母顺序
int m,n;
int flag;//0表示排好,1表示有环,-1表示没有排好
int ans;
void topsort()
{
int pos;
for(int i=1;i<=n;i++)
{
du2[i]=du[i];//临时记录度数
}
for(int i=1;i<=n;i++)
{
int s=0;
for(int j=1;j<=n;j++)
{
if(du2[j]==0)寻找度数为0的顶点
{
s++;
pos=j;
}
}
if(s==0) {flag=1;return;}如果再循环中没找到说明存在环
else if(s>1) {flag=-1;}//如果等于1,说明只有一个入度为0的点,大于一的话说明很多 也就无法排序
du2[pos]=-1;
a[ans++]='A'+pos-1;//记录
a[ans]='\0';
for(int j=1;j<=n;j++)//将跟刚才入度为0点有关系的边 入度减一
{
if(map[pos][j])
du2[j]--;
}
}
}
int main()
{
int i,j,k;
int sign;
while(~scanf("%d%d",&n,&m),n,m)
{
memset(map,0,sizeof(map));
memset(a,0,sizeof(a));
memset(du,0,sizeof(du));
sign=0;//标记是否已经找到了关系
for(i=1;i<=m;i++)
{
flag=0;
ans=0;
scanf("%s",str);
if(sign==1) continue;//如果已经出结果 就不许要执行下一步了 但是还得输入进去
du[str[2]-'A'+1]++;
map[str[0]-'A'+1][str[2]-'A'+1]=1;
topsort();
if(flag==1) {printf("Inconsistency found after %d relations.\n",i);sign=1;}
if(flag==0)
{
//for(int j=0;j<ans;j++)
printf("Sorted sequence determined after %d relations: %s.\n",i,a);
sign=1;
}
}
if(flag==-1) printf("Sorted sequence cannot be determined.\n");
}
return 0;
}