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.
思路:
拓扑排序:在一张图中找到入度为0的点拿掉,再找,在哪掉,最后拿空了,则拓扑成功, 拿的顺序就是拓扑排序结果。(写自己的模板,让别人看了晕去吧!!!)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <vector> using namespace std; vector<int> map[101], out; int indegree[101], tempin[101]; int tsort(int n) { queue<int> q; int i, temp, var, flag = 0; for(i = 0; i < n; i++) //把所有入度为0的点加入队列(最后一步会入度=0只有一个) { if(indegree[i] == 0) { q.push(i); } } memcpy(tempin, indegree, sizeof(indegree)); //备份一个indegree, 后面操作会改动indegree out.clear(); //清理存储拓扑下来的点(拓扑序列) while(!q.empty()) { if(q.size() > 1) //若能拓扑成功最后一次是一条链, q.size() == 1 { flag = 1; } var = q.front(); q.pop(); out.push_back(var); for(i = 0; i < map[var].size(); i++) //把每个点的下一个入度为零的点存入队列 { temp = map[var][i]; tempin[temp]--; //拿走边,下一节点入度减一 if(tempin[temp] == 0) { q.push(temp); } } } if(out.size() != n) //判断回路 { return 1; } if(flag) //判断是否拓扑成功 { return 0; } return 2; //拓扑成功 } int main() { int n, m, i, flag, j; char str[5]; while(scanf("%d%d", &n, &m) && (m+n)) { flag = 0; memset(indegree, 0, sizeof(indegree)); for(i = 0, j = 0; i < m; i++) { scanf("%s", str); if(!flag) //判断是否需要继续tsort { map[str[0]-'A'].push_back(str[2]-'A'); indegree[str[2]-'A']++; flag = tsort(n); j++; } } switch(flag) //打印结果 { case 0: printf("Sorted sequence cannot be determined.\n"); break; case 1: printf("Inconsistency found after %d relations.\n", j); break; case 2: printf("Sorted sequence determined after %d relations: ", j); for(i = 0; i < out.size(); i++) { printf("%c", out[i]+'A'); } printf(".\n"); break; } for(i = 0; i < n; i++) //清理map { map[i].clear(); } } return 0; }
-
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.