nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
-
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 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.
- 输出
- 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.
- 样例输入
-
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
- 样例输出
-
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
/** 拓扑排序实现步骤: ①、在有向图中找到没有前驱节点的顶点并输出 ②、删除该点和该点所到的下一个点之间的线 ③、重复①、②操作 **/
1 /** 2 分析: 3 Ⅰ、数据 n 表示有 n 个字母, 且字母由A开始到 char('A' + n - 1) 4 Ⅱ、 数据 m 表示有 m 个输入, 用来判断是否能够确定前 n 个字符的关系 5 Ⅲ、如果确定其前 n 个字母的先后顺序就输出,同时对后面的数据不做判断 6 Ⅳ、如果能够确定有环的存在也可以不用对后面的数据进行判断 (输出 Inconsistency...) 7 **/
核心代码:
1 int topo_sort () { 2 int flag = 1, temp [30], c = 0, Q[30], in_num, pos; 3 for (int i = 1; i <= n; ++ i) { 4 temp [i] = my_in [i]; 5 } 6 for (int i = 1; i <= n; ++ i) { 7 in_num = 0; 8 for (int j = 1; j <= n; ++ j) { 9 if (!temp [j]) { 10 pos = j; 11 in_num ++; 12 } 13 } 14 if (!in_num) return 0; // 环 15 if (in_num > 1) flag = -1; // 不可能有序,但有可能有环,所以不能return 16 17 Q [c ++] = pos; 18 temp [pos] = -1; 19 for (int j = 1; j <= n; ++ j) { 20 if (my_map [pos][j] == 1) { 21 temp [j] --; 22 } 23 } 24 } 25 return flag; 26 }
C/C++代码实现(AC):
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 6 using namespace std; 7 8 int n, m, my_map [30][30], my_in [30], Q[30], c; 9 10 int topo_sort () { 11 int flag = 1, temp [30], num_in, pos; 12 c = 0; 13 for (int i = 1; i <= n; ++ i) { 14 temp [i] = my_in [i]; 15 } 16 17 for (int i = 1; i <= n; ++ i) { 18 num_in = 0; 19 for (int j = 1; j <= n; ++ j) { 20 if (!temp [j]) { 21 ++ num_in; 22 pos = j; 23 } 24 } 25 if (!num_in) return 0; // 环 26 if (num_in > 1) flag = -1; // 不能确定 27 28 temp [pos] = -1; 29 Q [c ++] = pos; 30 for (int j = 1; j <= n; ++ j) { 31 if (my_map [pos][j] == 1) { 32 temp [j] --; 33 } 34 } 35 } 36 return flag; 37 } 38 39 int main () { 40 while (scanf ("%d%d", &n, &m), n != 0 || m != 0) { 41 int flag = 1, a1, b1; 42 char a, b, d; 43 memset (my_map, 0, sizeof (my_map)); 44 memset (my_in, 0, sizeof (my_in)); 45 46 for (int i = 1; i <= m; ++ i) { 47 getchar (); 48 scanf ("%c%c%c", &a, &d, &b); 49 if (!flag) continue; 50 51 a1 = int (a - 'A' + 1); 52 b1 = int (b - 'A' + 1); 53 my_map [a1][b1] = 1; 54 my_in [b1] ++; 55 56 int x = topo_sort (); 57 if (x == 1) { 58 printf("Sorted sequence determined after %d relations: ",i); 59 for (int i = 0; i < c; ++ i) { 60 printf ("%c", 'A' + Q [i] - 1); 61 } 62 printf (".\n"); 63 flag = 0; 64 } else if (x == 0) { 65 printf("Inconsistency found after %d relations.\n",i); 66 flag = 0; 67 } 68 } 69 70 if (flag) { 71 printf("Sorted sequence cannot be determined.\n"); 72 } 73 } 74 }