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.
先给两组关键性数据
6 6 A<F B<D C<E F<D D<E E<F output: Inconsistency found after 6 relations. 5 5 A<B B<C C<D D<E E<A output: Sorted sequence determined after 4 relations: ABCDE
第一个例子讲述的是:矛盾和多选,优先判断是否矛盾 第二个例子讲述的是:在矛盾之前如果有成功的,算是成功
拓扑排序的时候,我统计入度为0的个数,设入度数为0的节点有x个
1.如果x为0个,就说明有矛盾。
2.如果是x大于1个,就说明有多种选择。
但是这样带来的问题是,没法通过第一个样例,因为这个方法优先判断了多选
所以我用一个很暴力的方法(总共n就26嘛……):进行两次拓扑测试。
第一次不进行x是否大于1的测试。如果返回x=0,那么肯定矛盾了。如果返回x不为0,说明可能不确定,也可能存在排序。所以进行第二次测量,测量的内容是x为1还是大于1
刚看拓扑排序,这代码时仿照百度百科的。如果这个topo_sort有严重漏洞,还望不吝指出!
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <queue>
#include <string>
#include <stack>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define abs(x) ((x)>0?(x):-(x))
#define __max(a,b) ((a)>(b)?(a):(b))
#define __min(a,b) ((a)<(b)?(a):(b))
#define rep(i,repstt,repend) for(int i=repstt;i<repend;i++)
#define erep(i,repstt,repend) for(int i=repstt;i<=repend;i++)
#define inf 0x7f//2147483647
#define iinf 0x7fffffff
#define PI acos(-1.0)
#define NOBUG puts("No_Bug_Hear");
#define STOP system("pause");
#define FOUT freopen("out.txt","w",stdout);
#define FIN freopen("in.txt","r",stdin);
#define OUTCLOSE fclose(stdout);
#define INCLOSE fclose(stdin);
#define INIT(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
int into[30],ans[30],n,m;
char tmpa,tmpb;
bool f[30][30];
int topo_sort(int sz,int work){
INIT(into,0);
rep(i,0,sz)rep(j,0,sz)
if(f[i][j]){
into[j]++;
}
rep(i,0,sz){
int head,ok=0;
rep(j,0,sz)
if(!into[j]){
head=j;
ok++;
}
if(!ok)
return 0;
if(work==1&&ok>1)
return -1;
rep(k,0,sz)
if(f[head][k])
into[k]--;
ans[i]=head;
into[head]=-1;
}
return 1;
}
int main(){
//FIN
//FOUT
int res,i;
while(scanf("%d%d",&n,&m)&&(n+m)){
bool ud=1;
INIT(f,0);
for(i=0;i<m;i++){
scanf(" %c %*c %c",&tmpa,&tmpb);
f[tmpa-'A'][tmpb-'A']=1;
res=topo_sort(n,0);
if(res)
res=topo_sort(n,1);
if(res==1){
printf("Sorted sequence determined after %d relations: ",i+1);
rep(j,0,n)putchar(ans[j]+'A');
printf(".\n");
rep(j,i+1,m)scanf(" %*c %*c %*c");
ud=0;
break;
}else if(!res){
printf("Inconsistency found after %d relations.\n",i+1);
rep(j,i+1,m)scanf(" %*c %*c %*c");
ud=0;
break;
}
}
if(ud)printf("Sorted sequence cannot be determined.\n");
}
//INCLOSE
//OUTCLOSE
return 0;
}