poj 1094(拓扑排序)
2014-07-28 19:09
405 查看
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.
Sample Input
Sample Output
题目大意:给出一些字母的偏序,让你判断是否有矛盾,是否有多种情况;如果情况唯一输出结果;
分析:简单的图论问题,首先建立二维数组;存图(图不大,可以用二维数组储存),每次输入一个条件,将条件加入之后进行判断,如果满足,或者出现矛盾的话就输出,但是继续吧文件读完;在最后判断有没有满足条件,或者出现矛盾,没有的话就输出不能确定;
代码:
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.
题目大意:给出一些字母的偏序,让你判断是否有矛盾,是否有多种情况;如果情况唯一输出结果;
分析:简单的图论问题,首先建立二维数组;存图(图不大,可以用二维数组储存),每次输入一个条件,将条件加入之后进行判断,如果满足,或者出现矛盾的话就输出,但是继续吧文件读完;在最后判断有没有满足条件,或者出现矛盾,没有的话就输出不能确定;
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<stack> using namespace std; int ins[30],injudge[30],way[30]; int point,judge,where; int pic[30][30];///存图 int judges() { memcpy(injudge,ins,sizeof(ins));///将出入度复制过来; stack<int> s; for ( int i = 0; i < point; i++) { if ( injudge[i] == 0) s.push(i);///入度为0,就可以取下来为第一个;入栈; } int num = 0; int flag = 0; while ( !s.empty()) { if ( s.size() > 1){flag = true;}///如果有两个入度0 的点那么就不能确定路线 int temp = s.top(); s.pop(); way[num++] = temp;///将入度为1 的值计入路线中 for ( int i = 0; i < point;i++) { if ( pic[temp][i] && --injudge[i] == 0) s.push(i); ///如果有线,那么连接的点入度减去一,如果减去一之后为0 的话就入栈 } } if ( num != point ) return 1;///有圆圈 else if ( flag ) return 2;///不确定; return 3;///确定 } int main() { //freopen("in.txt","r",stdin); while ( scanf("%d %d",&point,&judge) != EOF ) { if ( point == 0 ) break; memset(ins,0,sizeof(ins)); memset(pic,0,sizeof(pic)); string j; string ans1=""; int flag1 = 0; int flag2 = 0; for ( int i = 0; i < judge; i++) { cin >> j; if (!flag1 && !flag2) { char a = j[0]; char b = j[1]; char c = j[2]; if ( pic[c-'A'][a-'A'] == 1)///如果有反线就输出矛盾; { flag2 = 1; printf("Inconsistency found after %d relations.\n",i+1); continue; } else { pic[a-'A'][c-'A'] = 1; ins[c-'A']++; } int flag3 = judges();///判断函数 if ( flag3 == 3 ) { printf("Sorted sequence determined after %d relations: ",i+1); for ( int k = 0; k < point; k++) { ans1 += way[k]+'A'; } cout << ans1 << "." << endl; flag1 = 1; }else if ( flag3 == 1) { printf("Inconsistency found after %d relations.\n",i+1); flag2 = 1; } } } if (!flag1&&!flag2) printf("Sorted sequence cannot be determined.\n"); } }
相关文章推荐
- POJ 1094: Sorting It All Out( 拓扑排序 )
- POJ 1094-Sorting It All Out(拓扑排序)
- POJ 1094(拓扑排序)
- poj 1094 Sorting It All Out(java + 拓扑排序)
- POJ1094 Sorting It All Out 拓扑排序(深搜)
- POJ 1094 拓扑排序
- POJ 1094 Sorting It All Out(拓扑排序)
- POJ - 1094(拓扑排序)
- POJ 1094 拓扑排序
- poj_1094 Sorting It All Out(拓扑排序 + floyd)
- POJ 1094 Sorting It All Out(拓扑排序)
- poj 1094 拓扑排序
- POJ1094《Sorting It All Out》方法:拓扑排序
- poj1094----拓扑排序
- POJ1094(拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)
- poj 1094 拓扑排序
- POJ 1094 Sorting It All Out (拓扑排序,有向图判环)
- poj 1094 拓扑排序
- POJ 1094 Sorting It All Out (拓扑排序)