|
|
|
@ -27,20 +27,19 @@ int check() {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
string getorder() { // 升序输出所有变量
|
|
|
|
|
char s[26];
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
// f[i][j] = 1表示i可以到达j (i< j)
|
|
|
|
|
for (int j = 0; j < n; j++) cnt += g[i][j]; // 比i大的有多少个
|
|
|
|
|
// 举个栗子:i=0,表示字符A
|
|
|
|
|
// 比如比i大的有5个,共6个字符:ABCDEF
|
|
|
|
|
// n - cnt - 1 = 6-5-1 = 0,也就是A放在第一个输出的位置上, 之所以再-1,是因为下标从0开始
|
|
|
|
|
s[n - cnt - 1] = i + 'A';
|
|
|
|
|
}
|
|
|
|
|
// 转s字符数组为字符串
|
|
|
|
|
return string(s, s + n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
@ -53,11 +52,11 @@ int main() {
|
|
|
|
|
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) { // 如果不存在矛盾,就尝试找出大小的顺序
|
|
|
|
|
g[a][b] = 1; // 有边
|
|
|
|
|
floyd(); // 求传递闭包
|
|
|
|
|
type = check(); // 检查是不是存在矛盾,或者找到了完整的顺序
|
|
|
|
|
if (type > 0) t = i; // 如果找到了顺序,或者发现了矛盾,记录是第几次输入后发现的
|
|
|
|
|
}
|
|
|
|
|
// 即使存在矛盾,也需要继续读入,直到本轮数据读入完成
|
|
|
|
|
}
|
|
|
|
@ -67,10 +66,8 @@ int main() {
|
|
|
|
|
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");
|
|
|
|
|
string ans = getorder(); // 输出升序排列的所有变量
|
|
|
|
|
printf("Sorted sequence determined after %d relations: %s.\n", t, ans.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|