本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15558203.html

题目链接

考虑每次都做一次拓扑排序。

如果所有节点未遍历,即存在环。

否则的话,如果结果唯一,即拓扑层数为 nn,判断队尾层数是否为 nn 即可。

否则结果不唯一。

由于最多只有26个字母,所以时间过得去。

——————————————————————————————————
说一下我做题时的几个坑点:

  1. 每次做拓扑排序时不要修改入度。

  2. 输出的是最早能体现出的操作。

至于漏.: 什么的,推荐使用cp editor 自己手动查找。

Code:
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
// Problem: P1347 排序
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1347
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define mo
#define N 35
struct node
{
int x, y, n;
}d[N*N];
int n, m, i, j, k;
int h[N], rd[N], ceng[N], q[N], dd[N];
int f, r, x, y, g;
char s[5];

void cun(int x, int y)
{
// printf("%d %d\n", x, y);
++k; d[k].x=x; d[k].y=y;
d[k].n=h[x]; h[x]=k;
++dd[y];
}

int tuopu(int p, int t)
{
f=r=0;
for(int i=1; i<=n; ++i) rd[i]=dd[i];
for(int i=1; i<=n; ++i)
if(rd[i]==0) q[++r]=i, ceng[i]=1;
while(f<r)
{
x=q[++f];
// printf("%d ", x);
for(g=h[x]; g; g=d[g].n)
{
y=d[g].y;
if((--rd[y])==0)
q[++r]=y, ceng[y]=ceng[x]+1;
}
}
// printf("\n");
if(ceng[q[r]]==n)
{
printf("Sorted sequence determined after %d relations: ", t);
for(int i=1; i<=n; ++i) printf("%c", q[i]+'A'-1);
putchar('.');
exit(0);
}
if(p==0) return r!=n;
printf("Sorted sequence cannot be determined.");

}

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read(); m=read();
for(i=1; i<=m; ++i)
{
scanf("%s", s+1);
cun(s[1]-'A'+1, s[3]-'A'+1);
if(tuopu(0, i))
return printf("Inconsistency found after %d relations.", i), 0;
}
tuopu(1, n);
return 0;
}