本地爆稳,提交全RE
  • 板块P1347 排序
  • 楼主ynyxxx
  • 当前回复4
  • 已保存回复4
  • 发布时间2020/10/27 19:16
  • 上次更新2023/11/5 09:43:48
查看原帖
本地爆稳,提交全RE
27607
ynyxxx楼主2020/10/27 19:16

我真棒!

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
inline ll read()
{
	bool f=1;ll s=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();}
	while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^'0'),ch=getchar();
	return f?s:-s;	
}
ll n,m,head[10010],cnt,x,y,sh,inn[1010],ans[10100],ii,outt[1010];
bool vis[10010];
struct Node{ll to,nxt;}a[1000010];
inline void add(ll from,ll to)
{
	a[++cnt].to=to;
	a[cnt].nxt=head[from];
	head[from]=cnt;
}
inline bool ser(ll d,ll tx)
{
	if(vis[tx]){printf("Inconsistency found after %lld relations.",ii);return 0;}
	ans[d]=tx;
	if(d==n)
	{
		printf("Sorted sequence determined after %lld relations:" ,ii);
		for(int i=1;i<=n;i++)printf("%c",ans[i]+'A');printf(".");
		return 0;
	}
	vis[tx]=1;
	for(int i=head[tx];i;i=a[i].nxt)if(!ser(d+1,a[i].to))return 0;
	vis[tx]=0;return 1;
}
int main()
{
	n=read();m=read();sh=0;outt[n]=1;
	for(ii=1;ii<=m;ii++)
	{
		x=getchar()-'A',getchar(),y=getchar()-'A',getchar();
		add(x,y);
		inn[y]++;outt[x]++;
		while(inn[sh]||((!inn[sh])&&(!outt[sh])))sh++;
		if(sh==n){printf("Inconsistency found after %lld relations.",ii);return 0;}
		if(!ser(1,sh))return 0;
	}
	printf("Sorted sequence cannot be determined.");
	return 0;
}
2020/10/27 19:16
加载中...