diff --git a/TangDou/AcWing_TiGao/T3/Floyd/343.cpp b/TangDou/AcWing_TiGao/T3/Floyd/343.cpp deleted file mode 100644 index 9c69f46..0000000 --- a/TangDou/AcWing_TiGao/T3/Floyd/343.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include -using namespace std; - -// Floyd解决传送闭包问题 -const int N = 27; -int n; // n个变量 -int m; // m个不等式 -int f[N][N]; // 传递闭包结果 - -void floyd() { - for (int k = 0; k < n; k++) - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - f[i][j] |= f[i][k] & f[k][j]; // i可以到达k,k可以到达j,那么i可以到达j -} -// 1:可以确定两两之间的关系,2:矛盾,3:不能确定两两之间的关系 -int check() { - // 如果i> n >> m, n && m) { - string S; - int k = 3; // 3:不能确定两两之间的关系 - memset(f, 0, sizeof f); // 初始化邻接矩阵 - // m条边 - for (int i = 1; i <= m; i++) { - cin >> S; - // 已确定或者出现了矛盾,就没有必要再处理了,但是,还需要耐心的读取完毕,因为可能还有下一轮,不读入完耽误下一轮 - if (k < 3) continue; - // 变量只可能为大写字母A~Z,映射到0~25 - int a = S[0] - 'A', b = S[2] - 'A'; - f[a][b] = 1; // 记录a +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> 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; +} \ No newline at end of file diff --git a/TangDou/AcWing_TiGao/T3/Floyd/343_2.cpp b/TangDou/AcWing_TiGao/T3/Floyd/343_2.cpp new file mode 100644 index 0000000..4dc24bf --- /dev/null +++ b/TangDou/AcWing_TiGao/T3/Floyd/343_2.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; + +const int N = 26; + +int n, m; +bool d[N][N]; +bool st[N]; + +int check() { + for (int i = 0; i < n; i++) + if (d[i][i]) return 2; + + for (int i = 0; i < n; i++) + for (int j = 0; j < i; j++) + if (!d[i][j] && !d[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] && d[j][i]) { + flag = false; + break; + } + if (flag) { + st[i] = true; + return 'A' + i; + } + } +} + +int main() { + while (cin >> n >> m, n || m) { + memset(d, 0, sizeof d); + + 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'; + + if (!type) { + d[a][b] = 1; + for (int x = 0; x < n; x++) { + if (d[x][a]) d[x][b] = 1; + if (d[b][x]) d[a][x] = 1; + for (int y = 0; y < n; y++) + if (d[x][a] && d[b][y]) + d[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 { + 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; +} \ No newline at end of file