悬关
#include <bits/stdc++.h>
using namespace std;
const int MAXC = 26;
int n, m, f[MAXC], F[MAXC];
vector<int> s[MAXC];
string S(){
for(int i = 0; i < n; i++){
f[i] = F[i];
}
queue<int> g;
for(int i = 0; i < n; i++){
if(!f[i]){
g.push(i);
}
}
string ans = "";
int kkk = 0;
while(!g.empty()){
if(g.size() != 1){
return "z";
}
int x = g.front();g.pop();
ans += x + 'A';++kkk;
for(int i = 0; i < s[x].size(); i++){
--f[s[x][i]];
if(!f[s[x][i]]){
g.push(s[x][i]);
}
}
}
if(kkk != n) {return "y";}
return ans;
}
int main(){
cin >> n >> m;
for(int i = 1; i <= m; i++){
char a, b;cin >> a >> b >> b;
s[a - 'A'].push_back(b - 'A');
++F[b - 'A'];string x = S();
if(x == "y") {
cout << "Inconsistency found after " << i << " relations.";
return 0;
}
if(x != "z"){
cout << "Sorted sequence determined after " << to_string(i) << " relations: " << x << ".";
return 0;
}
}
cout << "Sorted sequence cannot be determined.";
return 0;
}