|
|
#include <bits/stdc++.h>
|
|
|
using namespace std;
|
|
|
|
|
|
const int N = 26;
|
|
|
|
|
|
int n, m;
|
|
|
int g[N][N];
|
|
|
bool st[N];
|
|
|
|
|
|
// 求传递闭包
|
|
|
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() {
|
|
|
for (int i = 0; i < n; i++)
|
|
|
if (g[i][i]) return 2; // 矛盾
|
|
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
for (int j = 0; j < i; j++)
|
|
|
if (!g[i][j] && !g[j][i]) // 待继续
|
|
|
return 0;
|
|
|
|
|
|
return 1; // 找到顺序
|
|
|
}
|
|
|
|
|
|
char get_min() {
|
|
|
for (int i = 0; i < n; i++)
|
|
|
if (!st[i]) {
|
|
|
bool flag = true;
|
|
|
for (int j = 0; j < n; j++)
|
|
|
if (!st[j] && g[j][i]) { // 如果存在j<i的点,那么i点不是最小的点
|
|
|
flag = false;
|
|
|
break;
|
|
|
}
|
|
|
if (flag) {
|
|
|
st[i] = true;
|
|
|
return 'A' + i;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int main() {
|
|
|
while (cin >> n >> m, n || m) {
|
|
|
memset(g, 0, sizeof g); // 邻接矩阵
|
|
|
int type = 0, t; // type: 0=还需要继续给出条件 1=找到了顺序 2=存在冲突
|
|
|
// t:在第几次输入后找到了顺序,不能中间break,因为那样会造成数据无法完成读入,后续的操作无法进行,只能记录下来当时的i
|
|
|
for (int i = 1; i <= m; i++) {
|
|
|
char s[5];
|
|
|
cin >> s;
|
|
|
int a = s[0] - 'A', b = s[2] - 'A'; // A->0,B->1,...,Z->25完成映射关系
|
|
|
|
|
|
if (!type) { // 如果不存在矛盾,就尝试找出大小的顺序
|
|
|
g[a][b] = 1; // 有边
|
|
|
floyd(); // 求传递闭包
|
|
|
type = check(); // 检查是不是存在矛盾,或者找到了完整的顺序
|
|
|
if (type) t = i; // 如果找到了顺序,记录是第几次输入后找到顺序
|
|
|
}
|
|
|
// 即使存在矛盾,也需要继续读入,直到本轮数据读入完成
|
|
|
}
|
|
|
|
|
|
if (!type)
|
|
|
puts("Sorted sequence cannot be determined.");
|
|
|
else if (type == 2)
|
|
|
printf("Inconsistency found after %d relations.\n", t);
|
|
|
else {
|
|
|
memset(st, 0, sizeof st);
|
|
|
printf("Sorted sequence determined after %d relations: ", t);
|
|
|
for (int i = 0; i < n; i++) printf("%c", get_min());
|
|
|
printf(".\n");
|
|
|
}
|
|
|
}
|
|
|
return 0;
|
|
|
} |