【洛谷P1347 排序】题解

题目链接

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

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

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

否则结果不唯一。

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

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

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

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

至于漏.: 什么的,推荐使用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<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; 
}

posted @ 2021-11-15 20:56  zhangtingxi  阅读(74)  评论(0编辑  收藏  举报