P1347 排序
思路:每加一条边,更新结点数并做一次拓扑排序判断有没有环,并同时判断能不能唯一确定一个大小关系序列。
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int N = 27;
int n, m;
int st[N], in[N], backup[N];
int cur;
int g[N][N];
vector<char> res;
int flag;
int topsort(){
memcpy(backup, in, sizeof in);
queue<int> q;
flag = 0;
res.clear();
int cnt = 0;
for(int i = 1; i <= n; i ++)
if(in[i] == 0 && st[i]){
q.push(i);
cnt ++;
}
if(cnt > 1) flag = 1;
while(q.size()){
int h = q.front();
q.pop();
res.push_back('A' + h - 1);
int t = 0;
for(int i = 1; i <= n; i ++){
if(g[h][i]){
in[i] --;
if(in[i] == 0){
q.push(i);
t ++;
cnt ++;
if(t > 1) flag = 1;
}
}
}
}
memcpy(in, backup, sizeof in);
return cur == cnt;
}
int main(){
cin >> n >> m;
for(int i = 1; i <= m; i ++){
char a, b, c;
cin >> a >> b >> c;
int av = a - 'A' + 1, cv = c - 'A' + 1;
if(st[av] == 0) st[av] = 1, cur ++;
if(st[cv] == 0) st[cv] = 1, cur ++;
if(g[av][cv] == 0){
g[av][cv] = 1;
in[cv] ++;
}
int k = topsort();
if(k && !flag && cur == n){
printf("Sorted sequence determined after %d relations: ", i);
for(int j = 0; j < res.size(); j ++) cout << res[j];
cout << '.';
return 0;
}
if(!k){
printf("Inconsistency found after %d relations.", i);
return 0;
}
}
cout << "Sorted sequence cannot be determined.";
return 0;
}