poj1094 Sorting It All Outfloyd传递闭包拓扑序
Posted wyboooo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1094 Sorting It All Outfloyd传递闭包拓扑序相关的知识,希望对你有一定的参考价值。
Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:39731 | Accepted: 13975 |
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.
Source
题意:
给定关于n个字母的对应优先级关系,问是否可以根据优先级对他们进行排序。并且要求输出是在第几组可以得出结果。
会有优先级不一致和无法排序的情况。
思路:
感觉这题数据很迷啊,m的范围都不给我都不好估计复杂度。
以及,要注意输出时候的句号。特别是可以sort的时候。
是一道传递闭包的问题,用邻接矩阵建图,如果$A<B$,则表示$A$到$B$有一条有向边,$g[‘A‘][‘B‘] = 1$
每次添加一个关系,使用一次floyd计算能否得出结果,或者是否出现不一致。
输出排序结果的时候,其实只需要比较每个点的入度,按照入度从大到小排序就行了。
因为最后的矩阵是一个传递闭包,最小的那个字母肯定小于其他所有字母,也就是说他这一行会有$n-1$个$1$
1 #include<iostream> 2 //#include<bits/stdc++.h> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstdlib> 6 #include<cstring> 7 #include<algorithm> 8 #include<queue> 9 #include<vector> 10 #include<set> 11 #include<climits> 12 using namespace std; 13 typedef long long LL; 14 #define N 100010 15 #define pi 3.1415926535 16 #define inf 0x3f3f3f3f 17 18 int n, m; 19 int g[30][30], tmpg[30][30]; 20 21 int floyd() 22 { 23 for(int i = 0; i < n; i++){ 24 for(int j = 0; j < n; j++){ 25 tmpg[i][j] = g[i][j]; 26 } 27 } 28 for(int k = 0; k < n; k++){ 29 for(int i = 0; i < n; i++){ 30 for(int j = 0; j < n; j++){ 31 tmpg[i][j] |= tmpg[i][k] & tmpg[k][j]; 32 if(tmpg[i][j] == tmpg[j][i] && tmpg[i][j] == 1 && i != j){ 33 return -1; 34 } 35 } 36 } 37 } 38 for(int i = 0; i < n; i++){ 39 for(int j = 0; j < n; j++){ 40 if(tmpg[i][j] == tmpg[j][i] && tmpg[i][j] == 0 && i != j){ 41 return 0; 42 } 43 } 44 } 45 return 1; 46 } 47 48 struct node{ 49 int deg; 50 char ch; 51 }; 52 bool cmp(node a, node b) 53 { 54 return a.deg > b.deg; 55 } 56 57 void print() 58 { 59 //int deg[30]; 60 //memset(deg, 0, sizeof(deg)); 61 node character[30]; 62 for(int i = 0; i < n; i++){ 63 character[i].deg = 0; 64 character[i].ch = ‘A‘ + i; 65 } 66 for(int i = 0; i < n; i++){ 67 for(int j = 0; j < n; j++){ 68 if(tmpg[i][j]){ 69 character[i].deg++; 70 } 71 } 72 } 73 sort(character, character + n, cmp); 74 for(int i = 0; i < n; i++){ 75 printf("%c", character[i].ch); 76 } 77 printf(". "); 78 79 /*queue<int>que; 80 for(int i = 0; i < n; i++){ 81 printf("%d ", deg[i]); 82 if(!deg[i]){ 83 que.push(i); 84 break; 85 } 86 } 87 for(int i = 0; i < n; i++){ 88 int x = que.front();que.pop(); 89 printf("%c", x + ‘A‘); 90 for(int k = 0; k < n; k++){ 91 if(tmpg[k][x])deg[k]--; 92 if(!deg[k])que.push(k); 93 } 94 }*/ 95 } 96 97 int main() 98 { 99 while(scanf("%d%d", &n, &m) != EOF && n || m){ 100 memset(g, 0, sizeof(g)); 101 /*for(int i = 0; i < n; i++){ 102 g[i][i] = 1; 103 }*/ 104 105 int i; 106 bool dont = false; 107 for(i = 1; i <= m; i++){ 108 char a, b; 109 getchar(); 110 scanf("%c<%c", &a, &b); 111 g[a - ‘A‘][b - ‘A‘] = 1; 112 if(!dont){ 113 int flag = floyd(); 114 if(flag == -1){ 115 printf("Inconsistency found after %d relations. ", i); 116 dont = true; 117 } 118 else if(flag == 1){ 119 printf("Sorted sequence determined after %d relations: ", i); 120 print(); 121 dont = true; 122 } 123 } 124 } 125 if(i > m && !dont){ 126 printf("Sorted sequence cannot be determined. "); 127 } 128 } 129 return 0; 130 }
以上是关于poj1094 Sorting It All Outfloyd传递闭包拓扑序的主要内容,如果未能解决你的问题,请参考以下文章
POJ 1094 Sorting It All Out(拓扑排序判环)
POJ 1094: Sorting It All Out( 拓扑排序 )