拓扑排序,用个数组模拟水一水也是可以。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#ifndef ONLINE_JUDGE
#include <windows.h>
#endif
using namespace std;
const int MAX = 30;
int G[MAX][MAX];
bool vis[MAX][MAX];
int n, m;
int go(int f, int t) {
if (vis[f][t]) return G[f][t] == 1 ? 1 : -1;
vis[f][t] = true; //加个判重,否则递归递归栈过深,会TLE
if (G[f][t] == -1) G[f][t] = 1;
else if (G[f][t] == 0) return -1;
if (G[t][f] == -1) G[t][f] = 0;
else if (G[t][f] == 1) return -1;
for (int i = 0; i < n; ++i) {
if (G[t][i] == 1) {
if (go(f, i) == -1) return -1;
}
}
for (int i = 0; i < n; ++i) {
if (G[i][f] == 1) {
if (go(i, t) == -1) return -1;
}
}
return 1;
}
bool check(void) {
int s = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (G[i][j] == 1) ++s;
}
}
return s == n * (n - 1) / 2;
}
void getSort(void) {
int rank[MAX] = {0};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (G[i][j] == 1) rank[i]++;
}
}
for (int pa = n - 1; pa >= 0; --pa) {
for (int j = 0; j < n; ++j) {
if (rank[j] == pa) {
putchar(j + 'A');
break;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("r.in", "r", stdin);
#endif
while (scanf(" %d %d", &n, &m) && (n | m)) {
memset(G, -1, sizeof(G));
memset(vis, false, sizeof(vis));
char str[10];
int flag = -1, step;
for (int i = 1; i <= m; ++i) {
scanf(" %s", str);
if (flag != -1) continue;
int x = str[0] - 'A', y = str[2] - 'A';
if (go(x, y) == -1) {
flag = 0;
step = i;
}
if (check()) {
flag = 1;
step = i;
}
//Debug();
}
if (flag == -1) puts("Sorted sequence cannot be determined.");
else if (flag == 0) {
printf("Inconsistency found after %d relations.\n", step);
} else {
printf("Sorted sequence determined after %d relations: ", step);
getSort();
puts(".");
}
}
return 0;
}