Files
python/TangDou/AcWing_TiGao/T3/Floyd/343_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

75 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 26;
int n, m;
bool g[N][N];
bool st[N];
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; // 找到顺序
}
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字符数组为字符串
string res;
for (int i = 0; i < n; i++) res = res + s[i];
return res;
}
int main() {
while (cin >> n >> m, n || m) {
memset(g, 0, sizeof g);
int type = 0, t;
for (int i = 1; i <= m; i++) {
char str[5];
cin >> str;
int a = str[0] - 'A', b = str[2] - 'A';
// a<b,那么,不需要完全的重新计算完整的传递闭包,只需要把与a,b相关的变更进行记录大小关系即可
if (!type) {
g[a][b] = 1;
for (int x = 0; x < n; x++) {
if (g[x][a]) g[x][b] = 1; // 所有比a小的x,一定比b小
if (g[b][x]) g[a][x] = 1; // 所有比b大的x,一定比a大
for (int y = 0; y < n; y++)
if (g[x][a] && g[b][y])
g[x][y] = 1;
}
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 {
string ans = getorder(); // 输出升序排列的所有变量
printf("Sorted sequence determined after %d relations: %s.\n", t, ans.c_str());
}
}
return 0;
}