您的位置:首页 > 其它

ZOJ 1060 (POJ 1094)  Sorting It …

2012-12-05 17:14 441 查看
Sorting It All OutTime Limit: 2 SecondsMemory Limit: 65536 KBAn ascending sorted sequence of distinct values is one in whichsome form of a less-than operator is used to order the elementsfrom 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 ofrelations of the form A < B and ask you to determinewhether a sorted order has been specified or not.InputInput consists of multiple problem instances. Each instancestarts with a line containing two positive integers n and m. thefirst value indicated the number of objects to sort, where 2<= n <= 26. The objects to be sortedwill be the first n characters of the uppercase alphabet. Thesecond 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 consistingof three characters: an uppercase letter, the character"<" and a second uppercase letter. No letter will beoutside the range of the first n letters of the alphabet. Values ofn = m = 0 indicate end of input.OutputFor each problem instance, output consists of one line. Thisline 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 timeeither a sorted sequence is determined or an inconsistency isfound, whichever comes first, and yyy...y is the sorted, ascendingsequence.Sample Input4 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0Sample OutputSorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.Source: East Central North America 2001关系闭包,说的那么玄乎,看了半天才知道不就是floyd嘛,愣让我好几天不敢做这题…… 题意:给定一系列大小比较,然后判定到哪一组的时候可以得出最后的比较结果,或者得出矛盾的结论 代码:
C语言:高亮代码由发芽网提供#include<stdio.h>#include<string.h>#include<stdlib.h>#define MAXN 27structpoint{intnum,time;}map[MAXN];intdir[MAXN][MAXN],n,m;intcmp(const void *a,const void *b){structpoint *c=(struct point *)a;structpoint *d=(struct point *)b;returnc->time - d->time;}voidfloyd(){inti,j,k;for(k=0;k<n;k++)for(i=0;i<n;i++)for(j=0;j<n;j++)dir[i][j]=(dir[i][j]||(dir[i][k]&&dir[k][j]));}intCircle(){inti;for(i=0;i<n;i++)if(dir[i][i])return1;return0;}intrange(){inti,j;for(i=0;i<n;i++){map[i].num=i;map[i].time=0;}for(i=0;i<n;i++)for(j=0;j<n;j++)if(dir[i][j])map[j].time++;qsort(map,n,sizeof(map[0]),cmp);for(i=0;i<n;i++)if(map[i].time == map[i+1].time)return0;return1;}intmain(){inti,ok_ans,no_ans,ok,circle;charstr[5];while(scanf("%d%d",&n,&m),n!=0||m!=0){ok=circle=ok_ans=no_ans=0;memset(dir,0,sizeof(dir));for(i=1;i<=m;i++){scanf("%s",str);if(ok||circle)continue;dir[str[0]-'A'][str[2]-'A']=1;floyd();circle=Circle();if(circle){no_ans=i;continue;}ok=range();if(ok)ok_ans=i;}if(ok){printf("Sorted sequence determined after %drelations: ",ok_ans);for(i=0;i<n;i++)printf("%c",map[i].num+'A');}elseif(circle)printf("Inconsistency found after %drelations",no_ans);elseprintf("Sorted sequence cannot bedetermined");printf(".\n");}return0;}
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: