Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 33871 | Accepted: 11858 |
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
这道拓扑排序题有两点需要注意的是,它是没输入一对比较后都要判断一次它是否可以排序或则是否矛盾,如果找到一个矛盾的或则成立的拓扑序列,则对后续输入不作处理,如果到输入的末尾都没找到上述两种情况则输出Sorted sequence cannot be determined.
对于判断部分,要注意判断如果队列中如果元素大于1
就一定无序,因为有序是一对一的,每次度为一的点只可能有一个
这道拓扑排序题有两点需要注意的是,它是没输入一对比较后都要判断一次它是否可以排序或则是否矛盾,如果找到一个矛盾的或则成立的拓扑序列,则对后续输入不作处理,如果到输入的末尾都没找到上述两种情况则输出Sorted sequence cannot be determined.
对于判断部分,要注意判断如果队列中如果元素大于1
就一定无序,因为有序是一对一的,每次度为一的点只可能有一个
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
int Map[30][30],in[30],in1[30],out[30],out1[30],vist[30];
int n,m,NUM1,NUM2,NUM3,num;
int flag1,flag2,flag3;
int str[30],cnt;
void pan(int Num)
{
if(flag3 || flag1)//如果有序或冲突则不做后续判断
return;
cnt = 0;
memset(str,0,sizeof(str));
queue<int>q;
flag2 = 0;
memset(vist,0,sizeof(vist));
for(int i = 1; i <= n; i++)
{
in1[i] = in[i];
out1[i] = out[i];
if(in1[i] == 0)
{
vist[i] = 1;
q.push(i);
}
}
int pos;
while(!q.empty())
{
int Q = q.size();
if(Q>1)
flag2 = 1;
pos = q.front();
if(!flag1)
str[cnt++] = pos;
q.pop();
for(int j = 1; j <= n; j++)
{
if(!vist[j] && Map[pos][j])
{
in1[j]--;
out1[pos]--;
if(in1[j] == 0)
{
vist[j] = 1;
q.push(j);
}
}
}
}
for(int i = 1; i <= n; i++)
{
if(in1[i])
{
flag3 = 1;
NUM3 = Num;
break;
}
}
if(!flag2 && !flag3 && !flag1)
{
flag1 = 1;
NUM1 = Num;
}
}
int main()
{
while(~scanf("%d %d%*c",&n,&m) && (n||m))
{
memset(Map,0,sizeof(Map));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
num = 0;
flag1 = flag3 = 0;
while(m--)
{
num++;
char A,B;
scanf("%c%*c%c%*c",&A,&B);
int x = A-'A'+1;
int y = B-'A'+1;
if(!Map[x][y])
{
Map[x][y] = 1;
in[y]++;
out[x]++;
}
pan(num);
}
if(flag3)
cout<<"Inconsistency found after "<<NUM3<<" relations."<<endl;
else if(flag1)
{
cout<<"Sorted sequence determined after "<<NUM1<<" relations: ";
for(int i = 0; i < cnt; i++)
printf("%c",'A'+str[i]-1);
cout<<"."<<endl;
}
else
cout<<"Sorted sequence cannot be determined."<<endl;
}
return 0;
}