一、内容
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.
二、思路
- 传递闭包
- 求一组合理的顺序, 当输入一组关系如A < B 那么我们就建立一条A---->B的边,若最终的顺序符合要求的话,那么必然任意2点之间都是有边的(u—>v 或 v---->u)仅有一条,若出现2条的话,那么求出传递闭包后就会出现自环。
- 1.矛盾:若出现自环就矛盾
- 2.有序:在1条件不成立的条件下,若任意2点之间都有边。
- 优化: 由于每次只会添加一条新边u—>v,那么我们只需要更新u,v有关的点即可
三、代码
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 26;
int n, m, t, d[N][N], g[N][N], in[N]; //d代表传递闭包 g代表原图用于拓扑序判断有边
char s[4];
void floyd() {
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i][j] |= g[i][k] & g[k][j];
}
}
}
}
int check() {
//有矛盾的情况 A<A 代表出现一个自环
for (int i = 0; i < n; i++) if (g[i][i]) return 2;
//有序序列的话 代表所有的2个点之间都能够有一条边 若有2点之间都没边 那么就没有找到
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (!g[i][j] && !g[j][i]) return 0;
}
}
return 1; //成功找到一条有序序列
}
void topsort() {
queue<int> q;
for (int i = 0; i < n; i++) if (!in[i]) q.push(i);
printf("Sorted sequence determined after %d relations: ", t);
while (!q.empty()) {
int u = q.front();
q.pop();
printf("%c", u + 'A');
for (int v = 0; v < n; v++) {
if (d[u][v]) {
in[v]--;
if (!in[v]) q.push(v);
}
}
}
printf(".\n");
}
int main() {
while (scanf("%d%d", &n, &m), n) {
memset(g, 0, sizeof(g));
memset(d, 0, sizeof(d));
memset(in, 0, sizeof(in));
int st = 0; //0代表没有找到矛盾或有序序列 1 代表找到有序 2找到矛盾
for (int i = 1; i <= m; i++) {
scanf("%s", s); //A < B 建立一条A->B的边
if (!st) {
int u = s[0] - 'A';
int v = s[2] - 'A';
in[v]++; //入度+1
g[u][v] = d[u][v] = 1;
floyd(); //更新传递闭包
st = check(); //更新状态
t = i;
}
}
if (!st) printf("Sorted sequence cannot be determined.\n");
else if (st == 1) topsort();
else printf("Inconsistency found after %d relations.\n", t);
}
return 0;
}
优化:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 26;
int n, m, t, d[N][N], g[N][N], in[N]; //d代表传递闭包 g代表原图用于拓扑序判断有边
char s[4];
int check() {
//有矛盾的情况 A<A 代表出现一个自环
for (int i = 0; i < n; i++) if (g[i][i]) return 2;
//有序序列的话 代表所有的2个点之间都能够有一条边 若有2点之间都没边 那么就没有找到
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (!g[i][j] && !g[j][i]) return 0;
}
}
return 1; //成功找到一条有序序列
}
void topsort() {
queue<int> q;
for (int i = 0; i < n; i++) if (!in[i]) q.push(i);
printf("Sorted sequence determined after %d relations: ", t);
while (!q.empty()) {
int u = q.front();
q.pop();
printf("%c", u + 'A');
for (int v = 0; v < n; v++) {
if (d[u][v]) {
in[v]--;
if (!in[v]) q.push(v);
}
}
}
printf(".\n");
}
int main() {
while (scanf("%d%d", &n, &m), n) {
memset(g, 0, sizeof(g));
memset(d, 0, sizeof(d));
memset(in, 0, sizeof(in));
int st = 0; //0代表没有找到矛盾或有序序列 1 代表找到有序 2找到矛盾
for (int i = 1; i <= m; i++) {
scanf("%s", s); //A < B 建立一条A->B的边
if (!st) {
int u = s[0] - 'A';
int v = s[2] - 'A';
in[v]++; //入度+1
g[u][v] = d[u][v] = 1;
for (int i = 0; i < n; i++) {
if (g[i][u]) g[i][v] = 1;
if (g[v][i]) g[u][i] = 1;
for (int j = 0; j < n; j++) {
if (g[i][u] && g[v][j]) g[i][j] = 1;
}
}
st = check(); //更新状态
t = i;
}
}
if (!st) printf("Sorted sequence cannot be determined.\n");
else if (st == 1) topsort();
else printf("Inconsistency found after %d relations.\n", t);
}
return 0;
}