POJ1094(模拟····拓扑)
2014-10-02 14:08
405 查看
Sorting It All Out
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status
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
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.
解题思路:
可以用拓扑排序,也可以开二维数组模拟。题意是说锁定前n个大写字母,然后给m个关系,问能否通过这m个关系求得
最后把前n个字母排成序。
最后有三种可能:1.出现环了,不能排成序。2.所有关系都用上之后也无法排出n个字母的顺序。
3.顺利的完成排序。
对于1和3两种情况,要记录何时下结论它不能排成序、它何时能排成序。
核心部分来了。我们首先要构造一个小于关系的二维数组。如果是小于号,那么topo(a ,b),否则topo(b , a)。下面以a<b为例:
topo操作:1.如果a>b,那么明显出问题了,最终排不成序。 2.如果i < a ,那么i 一定小于b 。 3.如果j 大于b ,那么j 一定大于a.。
最后两层循环,看是否存在无法判断的情况,否则的话就一定能排成序。
如果能排成序的话,我们要输出排好序的前n个字母,在输出前我们开一个数组f,并且对它排序,要求它满足小于关系·······这里
没搞懂,我觉得如果判断排好序的话直接输出就可以,再判断一次没意义·····可是不加最后的判断就WA······我再想想反例吧······
完整代码:
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status
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
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.
解题思路:
可以用拓扑排序,也可以开二维数组模拟。题意是说锁定前n个大写字母,然后给m个关系,问能否通过这m个关系求得
最后把前n个字母排成序。
最后有三种可能:1.出现环了,不能排成序。2.所有关系都用上之后也无法排出n个字母的顺序。
3.顺利的完成排序。
对于1和3两种情况,要记录何时下结论它不能排成序、它何时能排成序。
核心部分来了。我们首先要构造一个小于关系的二维数组。如果是小于号,那么topo(a ,b),否则topo(b , a)。下面以a<b为例:
topo操作:1.如果a>b,那么明显出问题了,最终排不成序。 2.如果i < a ,那么i 一定小于b 。 3.如果j 大于b ,那么j 一定大于a.。
最后两层循环,看是否存在无法判断的情况,否则的话就一定能排成序。
如果能排成序的话,我们要输出排好序的前n个字母,在输出前我们开一个数组f,并且对它排序,要求它满足小于关系·······这里
没搞懂,我觉得如果判断排好序的话直接输出就可以,再判断一次没意义·····可是不加最后的判断就WA······我再想想反例吧······
完整代码:
#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; const int maxn = 30; int small[maxn][maxn]; string str; int n , m , res; bool cmp(int a , int b) { return small[a][b]; } int topo(int a , int b) { if(small[b][a]) return 0; small[a][b] = 1; for(int i = 0 ; i < n ; i ++) if(small[i][a]) small[i][b] = 1; for(int i = 0 ; i < n ; i ++) if(small[b][i]) small[a][i] = 1; for(int i = 0 ; i < n ; i ++) { for(int j = 0 ; j < n ; j++) { if(small[i][a] && small[b][j]) small[i][j] = 1; } } for(int i = 0 ; i < n ; i ++) { for(int j = i + 1 ; j < n ; j ++) { if(!(small[i][j] || small[j][i])) return 2; } } return 1; } void input(int i) { char s[3]; for(int j = i + 1 ; j < m ; j++) cin >> s; } void output() { int f[maxn]; for(int i = 0 ; i < n ; i ++) f[i] = i; sort(f , f + n , cmp); // for(int i = 0 ; i < n ; i ++) // cout << f[i] << " " ; //cout << endl; for(int i = 0 ; i < n ; i ++) putchar(f[i] + 'A'); } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif while(~scanf("%d%d",&n,&m)) { if(n == 0 && m == 0) break; memset(small , 0 , sizeof(small)); for(int i = 0 ; i < m ; i ++) { cin >> str; int a = str[0] - 'A'; int b = str[2] - 'A'; if(str[1] == '<') res = topo(a , b); else res = topo(b , a); // cout << res << endl; if(res == 0) { input(i); printf("Inconsistency found after %d relations.\n" , i + 1); break; } if(res == 1) { input(i); printf("Sorted sequence determined after %d relations: " , i + 1); output(); printf(".\n"); break; } } if(res == 2) printf("Sorted sequence cannot be determined.\n"); } }
相关文章推荐
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
- [POJ 2014] Flow Layout · 模拟
- ZOJ 1066 Square Ice(POJ 1099)(找规律&模拟)
- WebDriver拾级而上·之十五 拖曳动作模拟
- poj 1828 Monkeys' Pride 模拟
- poj&nbsp;2190&nbsp;isbn&nbsp;模拟
- 求高手帮忙做ATM模拟系统代码,最好是用MyEclipse 或者C# ····
- POJ 1033 / Northeastern Europe 1998 Defragment (模拟&队列)
- POJ 3295 Tautology(似乎是模拟?…
- UVA 514 - Rails && POJ 1363 (堆栈模拟)
- UVa 442/HDU 1082/ZOJ 1094 Matrix Chain Multiplication(模拟&栈)
- WinIo 模拟键盘鼠标··
- joj 1089 &&zoj 1060&&poj 1094 以及wa的分析和数据
- POJ 1094 && ZOJ 1060 Sorting It All Out 【拓扑排序入门】
- POJ 1573 && hdu 1035 Robot Motion(简单模拟)
- POJ 3125 && Sicily 1443 Printer Queue(模拟/队列)
- POJ1000·A+B Problem
- POJ 1094 Sorting It All Out(经典拓扑,唯一排序)
- poj-3087-Shuffle'm Up-模拟
- poj 1094 Sorting It All Out (拓扑排序)