90pts,1#过不了求调
  • 板块P1347 排序
  • 楼主Saturation
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/4 14:42
  • 上次更新2024/8/4 16:28:40
查看原帖
90pts,1#过不了求调
1060212
Saturation楼主2024/8/4 14:42
#include<bits/stdc++.h>
using namespace std;
const int N=2*21000;
int n,m;
int len[N];
int d[N],e[N],h[N],ne[N],idx;
queue<int>q;
int aaa;
int dd[N];
int cnt=1;
int pos,ot[N];
bool pu[N];
bool st[N];
bool tc[N];
int cs[N];
int in()
{
	 int ans=0,ff=1;
	 int c=getchar();
	 for(;!isdigit(c);c=getchar())
	 {
		 if(c=='-')
		 {
			ff=-1;
		 }
	 }
	 for(;isdigit(c);c=getchar())
	 {
		 ans=ans*10+c-'0';
	 }
	 return ans*ff;
}
void out(int x)
{
	 if(x<0)
	 {
		 putchar('-');
		 x=-x;
	 }
	 if(x>=10)
	 {
		 out(x/10);
	 }
	 putchar(x%10+'0');
}
void add(int x,int y)
{
	e[++idx]=y;
	ne[idx]=h[x];
	h[x]=idx;
}
int topo()
{
	for(int i=1;i<=n;i++)
	{
		if(dd[i]==0&&st[i])
		{
			q.push(i);
			if(!pu[i]&&tc[cs[i]]==0)
			{
				tc[cs[i]]=1;
				ot[++pos]=i;
				pu[i]=1;
			}
		}
	}
	while(q.size())
	{
		int x=q.front();
		q.pop();
		for(int i=h[x];~i;i=ne[i])
		{
			int y=e[i];
			
			if(st[y])
			{
				dd[y]--;
				if(dd[y]==0)
				{
					q.push(y);
					if(tc[cs[x]+1]==0)
					{
						cs[y]=cs[x]+1;
						tc[cs[y]]=1;
						if(!pu[y])
						{
							cnt++;
							ot[++pos]=y;
							pu[y]=1;
						}
					}
				}
			}
		}
	}
/*	for(int i=1;i<=n;i++)
	{
		cout<<dd[i]<<" ";
	}
	cout<<endl;*/
	for(int i=1;i<=n;i++)
	{
		if(dd[i]!=0&&st[i])
		{
			return 0;
		}
	}
	if(cnt<n)
	{
		return 1;
	}
	else
	{
		return 2;
	}
}
int main()
{
//	freopen("os.in","r",stdin);
//	freopen("os.out","w",stdout);
	memset(h,-1,sizeof h);
	n=in();
	m=in();
	for(int i=1;i<=m;i++)
	{
		memset(cs,0,sizeof cs);
		memset(tc,0,sizeof tc);
		char a,charr,b;
		int x,y;
		cin>>a>>charr>>b;
		x=a-'A'+1;
		y=b-'A'+1;
		add(x,y);
		st[x]=1;
		st[y]=1;
		cs[x]=1;
		d[y]++;
		for(int j=1;j<=n;j++)
		{
			dd[j]=d[j];
		}
		int judge=0;
		judge=topo();
		/*if(i==25)
		{
			cout<<judge<<endl;
			for(int i=1;i<=pos;i++)
			{
				cout<<(char)(ot[i]+'A'-1)<<" ";	
			}
			cout<<endl;
			cout<<cnt<<endl;
		}*/
		aaa=judge;
		if(judge==0)
		{
			cout<<"Inconsistency found after "<<i<<" relations.";
			return 0;
		}
		else if(judge==2)
		{
			cout<<"Sorted sequence determined after "<<i<<" relations: ";
			for(int i=1;i<=pos;i++)
			{
				cout<<(char)(ot[i]+'A'-1);
			}
			cout<<'.';
			return 0;
		}
	}
	if(aaa==1)
	{
		cout<<"Sorted sequence cannot be determined.";
	}
	return 0;
}


2024/8/4 14:42
加载中...