# Sorting It All Out

144人阅读 评论(0)
Sorting It All Out
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19627 Accepted: 6711

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.

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

#include<stdio.h>
#define MAX 30
int n,m;
int data[MAX][MAX],sorted[MAX];
int topologic(int left[],int right[],int used[])
{
int i,j,k,total;
int count=0,order=1;
int judge[MAX],samearray[MAX];
for(i=1;i<MAX;i++)
{
judge[i]=right[i];
if(!used[i])
judge[i]=-1;
}
while(1)
{
i=1,total=0;
while(i<=n)
{
while(i<=n&&judge[i]!=0)
i++;
if(i>n)
break;
samearray[++total]=i;
judge[i]=-1;
sorted[++count]=i;
}
if(total==0)
break;
if(total>1)
order=0;
for(i=1;i<=total;i++)
{
j=samearray[i];
for(k=1;k<=left[j];k++)
judge[data[j][k]]--;
}
}
if(count<n)
{
for(i=1;i<=n;i++)
if(judge[i]>0)
return 0;
return -1;
}
if(count==n)
{
if(!order)
return -1;
return 1;
}
}
int main()
{
int i,j,x,y,flag,result;
char three[5];
int left[MAX],right[MAX],used[MAX];
int alternation[MAX*MAX][2];
while(scanf("%d %d",&n,&m)&&m&&n)
{
result=0;
for(i=0;i<MAX;i++)
{
left[i]=0;
right[i]=0;
used[i]=0;
sorted[i]=0;
}
for(i=0;i<m;i++)
{
scanf("%s",&three);
alternation[i][0]=three[0]-'A'+1;
alternation[i][1]=three[2]-'A'+1;
}
for(i=0;i<m;i++)
{
x=alternation[i][0];
y=alternation[i][1];
data[x][++left[x]]=y;
right[y]++;
used[x]=used[y]=1;
flag=topologic(left,right,used);
if(flag==1)
{
printf("Sorted sequence determined after %d relations: ",i+1);
for(j=1;j<=n;j++)
printf("%c",sorted[j]+'A'-1);
printf(".\n");
result=1;
break;
}
if(flag==0)
{
printf("Inconsistency found after %d relations.\n",i+1);
result=1;
break;
}
}
if(!result)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}


0
0

【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐（算法+实战）--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
文章分类
文章存档
评论排行