Sorting It All Out
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 26852 |
|
Accepted: 9262 |
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>
#include<stdlib.h>
#include<string.h>
#define maxn 100000000
int n,m,a[30][30],p,ci;
char tem[5],result[30];
void paixu(int x)
{
int i,j,k=0,ii;
int temp;
int rudu[30],chudu[30];
int pdian=0,fsure=0,pbian=0;
int use[30];
memset(use,0,sizeof(use));
memset(rudu,0,sizeof(rudu));
memset(chudu,0,sizeof(chudu));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j])
{
chudu[i]++;
rudu[j]++;
}
}
}
do
{
temp=0;
for(ii=0;ii<n;ii++)
{
if(rudu[ii]==0&&chudu[ii]>0)
{
temp++;
if(temp==1)
i=ii;
}
}
if(temp)
{
for(j=0;j<n;j++)
{
if(a[i][j])
{
rudu[j]--;
pbian++;
}
}
chudu[i]=0;
pdian++;
result[k++]=i+'A';
use[i]=1;
}
if(temp>1)
fsure=1;
}while(temp!=0);
if(pbian<x)
{
p=1;
ci=x;
}
else
{
if(fsure)
p=0;
else
{
if(pdian==n-1)
{
p=2;
ci=x;
for(i=0;i<n;i++)
if(use[i]==0)
break;
if(i<n)
result[k++]=i+'A';
result[k++]='\0';
}
else
p=0;
}
}
}
void print()
{
int i,j;
switch(p)
{
case 1:printf("Inconsistency found after %d relations.\n",ci);break;
case 2:printf("Sorted sequence determined after %d relations: ",ci);
printf("%s.\n",result);break;
case 0:printf("Sorted sequence cannot be determined.\n");break;
default:break;
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF&&n)
{
p=0;
memset(a,0,sizeof(a));
for(i=1;i<=m;i++)
{
scanf("%s",tem);
if(p==0)
{
a[tem[0]-'A'][tem[2]-'A']=1;
paixu(i);
}
}
print();
}
return 0;
}