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.
有一些未知数各不相等,给出一些两两比较的大小关系,问到第几个关系开始可以确定整体排序或出现矛盾,再或者所有关系都用过了也无法确定整体排序
每输入一对关系,如果判定有结果,则可以忽略后面输入数据,即使后面输入数据能改变结果,也不用管。所以应该每输入一个关系就去更新当前的图,然后进行一趟拓扑排序。一旦产生结果,再对后面的数据处理下,就可以输出结果。
(1)最终的图可以排序: 在输入结束前如果能得到最终的图(就是用这n个字母作为顶点,一个都不能少),而且最终得到的图无环只有唯一一个无前驱(即入度为0)的结点,但允许其子图有多个无前驱的结点。在这步输出排序后,不再对后续输入进行操作。
(2)输出矛盾: 在输入结束前如果最终图的子图有环,在这步输出矛盾后,不再对后续输入进行操作
(3)输出无法确认排序: 这种情况必须全部关系输入后才能确定,其中又有2种可能
①最终图的字母一个不缺,但是有多个无前驱结点
②输入结束了,但最终的图仍然字母不全,与无前驱结点的多少无关
#include <iostream> #include <string.h> using namespace std; int n,m,map[27][27],deg[27],str[27]; int DFS() { int temp[27]={0},k,c=0,flag=1; for(int i=0;i<n;i++) temp[i]=deg[i]; for(int i=0;i<n;i++) { int count1=0; for(int j=0;j<n;j++) if(temp[j]==0) { count1++; k=j; } if(count1==0)return 0;//有环,有反向边 if(count1>1)flag=-1;//有多个点的入度为0,成了一个无向图而不是一个有序的线性序列 str[c++]=k; temp[k]=-1; for(int j=0;j<n;j++) if(map[k][j]==1) temp[j]--; } return flag; } int main() { while(cin>>n>>m&&(n!=0&&m!=0)) { memset(map,0,sizeof(map)); memset(deg,0,sizeof(deg)); memset(str,0,sizeof(str)); char s[4]; int flag=0; for(int i=1;i<=m;i++) { cin>>s; if(flag==1)continue; map[s[0]-'A'][s[2]-'A']=1; deg[s[2]-'A']++; int temp=DFS(); if(temp==1) { cout<<"Sorted sequence determined after "<<i<<" relations: "; for(int j=0; j<n; j++) cout<<char(str[j]+'A'); cout<<"."<<endl; flag=1; } if(temp==0) { cout<<"Inconsistency found after "<<i<<" relations."<<endl; flag=1; } } if(flag==0) cout<<"Sorted sequence cannot be determined."<<endl; } return 0; }