1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
| //#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include "algorithm"
#include "cstdio"
#include "queue"
#include "set"
#include "cstring"
#include "string"
#include "map"
#include "vector"
#include "math.h"
#include "utility" // pair头文件
#define esp 1e-6
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<char, char> P;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 5;
const int M = 1e9 + 5;
int vis[100], ma[100][100], in[100];
string s;
int n, m;
int topo(){ // 每加进一个条件就要拓扑一次
s = ""; //拓扑序列, 要是加进某一个条件之后正好完成了此拓扑排序。 将其输出即可, 所以其为全局变量
int now[100]; // 储存此时的所有边的入度
memset(vis, 0, sizeof vis);
int to = 0, f = 1, p; // to 入度为0的点的个数, f 标记这些字母是否完成了拓扑排序
// p 指向最后一个入度为0的点, 在下面可以看到,
for (int i = 1; i <= n; i++){
now[i] = in[i]; // in 入度
}
for (int i = 1; i <= n; i++){
to = 0; // 每一次循环都要找入度为0的点
for (int j = 1; j <= n; j++){
if (!now[j] && !vis[j]){ //
to++;
p = j;
}
}
if (to == 0) //如果没有入度为0的点, 那就出现了矛盾
return 0;
if (to >= 2) // 大于等于两个就有多种情况。说明现在还未排好序, 所以标记个2
f = 2;
for (int i = 1; i <= n; i++){
if (ma[p][i])
now[i]--; // 正常套路, 让与p有关的边的入度减一
}
vis[p] = 1;
s += p + 'A' - 1;
}
return f; // 返回1或2 具体看下
}
int main(){
char a, b, f;
while(cin >> n >> m) {
if (!n && !m)
break;
memset(in, 0, sizeof in);
int flag = 1;
memset(ma, 0, sizeof ma);
for (int i = 1; i <= m; i++){
cin >> a >> f >> b;
if(f == '>')
swap(a, b);
int x = a - 'A' + 1; //这个处理技巧真的妙, 妙不可言
int y = b - 'A' + 1;
if (!ma[x][y]){
ma[x][y] = 1;
in[y]++;
}
x = topo(); // 这是返回值就排上用场了
if (flag){ // 当flag == 0时, 代表着结果已出, 其他就没必要输出了
if (x == 0){
flag = 0; // 标记flag 这个也很妙呀
printf("Inconsistency found after %d relations.\n", i);
}
else if (x == 1){
flag = 0;
printf("Sorted sequence determined after %d relations: ", i);
cout << s << ".\n";
}
}
}
if (flag)
puts("Sorted sequence cannot be determined.");
}
return 0;
}
|