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.成立,输出偏序关系。
2.不成立,因为有至少两个点的大小顺序不确定。
3.不成立,因为有至少两个点的偏序关系矛盾。
呵呵,前前后后花了5个小时才把这道题A掉。一定注意,每输入一组偏序关系都要判断一次。如果拓扑排序没有点入度为零,则一定存在矛盾,可以终止。如果在中途拓扑排序有不止一个点入度为零,则还不能下结论。如果中途已经发现了合法且包含所有点的偏序关系,则直接开始输出,不管后面合不合法。如果所有偏序关系读完了,也没有做出结论,则偏序关系不确定。并且要注意要把每组数据读完,否则没用到的数据就读到下一组数据去了(这里参考了网上另一个大神的写法,很巧妙)。
poj里面的discuss有一些数据和提示很实用,建议提交前尝试一下。
其实这道题的拓扑排序部分不难,难就难在结论的逻辑顺序上。
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int grid[30][30],m,n,flag,in[30],in0[30],ans[30],now; void tpsort() { int c,i,j,k,cnt; c=0; for (i=1;i<=n;i++) { in0[i]=in[i]; } for (i=1;i<=n;i++) { cnt=0; for (j=1;j<=n;j++) { if (in0[j]==0) { cnt++; k=j; } } if (cnt==0) { flag=2; return; } if (cnt>1) { flag=-1; } ans[c]=k; c++; in0[k]=-1; for (j=1;j<=n;j++) { if (grid[k][j]==1) { in0[j]--; } } } return; } int main() { char a0,b0,u; int j,a,b,sign,i; while(1) { memset(grid,0,sizeof(grid)); memset(in,0,sizeof(in)); memset(in0,0,sizeof(in0)); sign=0; scanf("%d%d",&n,&m); if (m==0&&n==0) { break; } for (i=0;i<=m-1;i++) { flag=0; getchar(); scanf("%c%c%c",&a0,&u,&b0); if (sign==1)//把数据读完 { continue; } a=a0-64; b=b0-64; grid[a][b]=1; in[b]++; if (flag==0) { tpsort(); } if (flag==2) { printf("Inconsistency found after %d relations.\n",i+1); sign=1; } else if (flag==0) { printf("Sorted sequence determined after %d relations: ",i+1); for(j=0;j<n;j++) { printf("%c",ans[j]+64); } sign=1; printf(".\n"); } } if (sign==0)//sign表示是否下结论了 { printf("Sorted sequence cannot be determined.\n"); } } return 0; }