voidfloyd() { 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]; } } } }
intcheck() { for (int i = 0; i < n; ++i) if (g[i][i]) return2;
for (int i = 0; i < n; ++i) for (int j = 0; j < i; ++j) if (!g[i][j] && !g[j][i]) { return0; }
return1; }
intget_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]) { flag = false; break; } } if (flag) { st[i] = true; return i; } } }
return0; }
intmain() { while(cin >> n >> m, n || m) { memset(g, 0, sizeof g); memset(st, false, sizeof st); int typ = 0, t; for (int i = 0; i < m; ++i) { char a[4]; cin >> a;
if (!typ) { g[a[0] - 'A'][a[2] - 'A'] = 1; floyd(); typ = check(); if (typ) t = i + 1; } } if (!typ) cout << "Sorted sequence cannot be determined.\n"; elseif (typ == 2) cout << "Inconsistency found after "<< t << " relations.\n"; else { cout << "Sorted sequence determined after "<< t<< " relations: "; for (int i = 0; i < n; ++i) cout << char('A' + get_min()); cout << ".\n"; } }