Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
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.
思路:
有点小麻烦,
1 就是题目要求的知道顺序,也就是在哪里就可以判断出顺序或者矛盾的关系,,这就需要我们一个一个判断了。
2 然后就是如何判断他们之间的关系了,这就要用到拓扑排序了,到最后若有环的出现,那么 最后的字符串肯定没有环中的字母,那么字符串个数就肯定小于n,如果一个队列里面的元素超过1的话,就代表着这个肯定不是一个对应另一个的,那么关系就是不清晰的。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 256;
const int maxm = 350;
vector<int> v[maxm];
string t, s[maxm];
int n, m;
int indeg[maxn] = {0};
int tosort() {
queue<int> q;
int indeg2[maxn] = {0};
bool book = false;
memcpy(indeg2, indeg, sizeof(indeg2));
for (int i = 0; i < n; i++) {
if (!indeg2[i]) q.push(i);
}
while (!q.empty()) {
if (q.size() > 1) book = true;
int x = q.front();
q.pop();
t += x + 'A';
for (int i = 0; i < v[x].size(); i++) {
if (--indeg2[v[x][i]] == 0) q.push(v[x][i]);
}
}
if (t.size() < n) return -1;
else if (book) return 1;
else return 0;
}
int main() {
while (scanf("%d %d", &n, &m) != EOF && n && m) {
int flag = 0, ans = 0;
memset(indeg, 0, sizeof(indeg));
for (int i = 0; i < n; i++) v[i].clear();
for (int i = 1; i <= m; i++) cin >> s[i];
for (int i = 1; i <= m; i++) {
t = "";
v[s[i][0] - 'A'].push_back(s[i][2] - 'A');
indeg[s[i][2] - 'A']++;
if ((flag = tosort()) == 0) {
ans = i;
break;
} else if ((flag = tosort()) == -1) {
ans = i;
break;
}
}
if (flag == 1) printf("Sorted sequence cannot be determined.\n");
else if (flag == -1) printf("Inconsistency found after %d relations.\n", ans);
else {
printf("Sorted sequence determined after %d relations: ", ans);
cout << t << ".\n";
}
}
return 0;
}