【洛谷P1347 排序】题解
题目链接
考虑每次都做一次拓扑排序。
如果所有节点未遍历,即存在环。
否则的话,如果结果唯一,即拓扑层数为 \(n\),判断队尾层数是否为 \(n\) 即可。
否则结果不唯一。
由于最多只有26个字母,所以时间过得去。
——————————————————————————————————
说一下我做题时的几个坑点:
- 
每次做拓扑排序时不要修改入度。
 - 
输出的是最早能体现出的操作。
 
至于漏.: 什么的,推荐使用cp editor 自己手动查找。
Code:
// 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
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