拓扑排序求调(悬关)
  • 板块灌水区
  • 楼主zh_gyy
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/21 12:36
  • 上次更新2023/11/2 12:17:06
查看原帖
拓扑排序求调(悬关)
999717
zh_gyy楼主2023/10/21 12:36

P1347 排序

#include<bits/stdc++.h>
using namespace std;
int n,m,a[30][30],d[30],f[30];
char ch;
void out(int x)
{
	printf("Sorted sequence determined after %d relations: ",x);
	for(int i=1;i<=n;++i)
	  cout<<char(f[i]+64);
	cout<<".";
	exit(0);
}
void find(int x)
{
	int ls[30];
	memcpy(ls,d,sizeof(d));
	for(int i=1;i<=n;++i)
	{
		/*for(int j=1;j<=n;++j)
		  cout<<ls[j]<<' ';
		cout<<'\n';
		int id=-1;*/
		int id=-1;
		for(int j=1;j<=n;++j)
		  if(ls[j]==0)
		    if(id==-1)
		      id=j;else
		      {
		      	//cout<<"\n"<<i<<'\n';
	  			return;
	  		  }
	  	if(id==-1)
	  	{
	  		//cout<<"\n"<<i<<'\n';
	  		return;	
		}
	  	f[i]=id;
	  	ls[id]=-1;
	  	for(int j=1;j<=n;++j)
	  	  if(a[i][j]==1)
	  	  {
	  	  	--ls[j];
		  }
	}
	out(x);
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;++i)
	  d[i]=-1;
	for(int i=1;i<=m;++i)
	{
		char s1,f,s2;
		cin>>s1>>f>>s2;
		int x1=s1-64;
		int x2=s2-64;
		if(a[x1][x2]==1)
		  continue;
		if(d[x1]==-1)
		  d[x1]=0;
		if(d[x2]==-1)
		  d[x2]=0;
		if(a[x1][x2]==0)
		  ++d[x2];
		a[x1][x2]=1;
		bool t1=true,t2=true;
		for(int j=1;j<=n;++j)
		  if(d[j]==0)
		   {
		   	t1=false;
		   	break;
			} 
		for(int j=1;j<=n;++j)
		  for(int k=j+1;k<=n;++k)
		    if(a[j][k]==1&&a[k][j]==1)
		    {
		    	t2=false;
		    	break;
			}
		if(t1||!t2)
		{
		    printf("Inconsistency found after %d relations.",i);
		    return 0;
		}
		find(i);
	}
	cout<<"Sorted sequence cannot be determined.";
	return 0;
 } 

其实就只是不知道怎么判断矛盾QwQ,救救我吧

2023/10/21 12:36
加载中...