40pts求调(代码恶心,勿吐槽)
查看原帖
40pts求调(代码恶心,勿吐槽)
729761
jiangyixuan120212楼主2025/1/18 18:13
#include<bits/stdc++.h>
using namespace std;
int n,m,i,f,x,b[30],in[30],fl;
vector<int>a[30];
queue<int>q;
char s[1000][10];
int pd(int x){
	int i,u,v,sum=0,sum1;
	for(i=0;i<=29;i++)a[i].clear();
	memset(in,0,sizeof(in));
	while(!q.empty())q.pop();
	for(i=1;i<=x;i++){
		u=s[i][0]-'A'+1;v=s[i][2]-'A'+1;
		a[u].push_back(v);
		in[v]++;
		if(u==v)return 2;
	}
	for(i=1;i<=n;i++)
		if(!in[i]){
			sum++;
			q.push(i);
		}
	if(sum>1)return -1;
	if(sum==0)return 2;
	sum=0;
	while(!q.empty()){
		int u=q.front();b[++sum]=u;q.pop();
//		if(x==4)cout<<u<<"\n";
		sum1=0;
		for(i=0;i<a[u].size();i++){
			int v=a[u][i];
			in[v]--;
			if(!in[v]){q.push(v),sum1++;
//			cout<<v<<"!\n";
			}
//			if(x==4)cout<<u<<" "<<v<<" "<<in[v]<<"\n";
		}
		if(sum1>1)return -1;
		if(sum1==0&&sum!=n)return 2;
	}
	return 1;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(i=1;i<=m;i++)cin>>s[i];
	fl=-1;
	for(i=1;i<=m;i++){
		x=pd(i);
		if(x==1){
			fl=1;
			break;
		}
		else if(x==2){
			fl=2;
			break;
		}
	}
	if(fl==1){
		cout<<"Sorted sequence determined after "<<i<<" relations: ";
		for(i=1;i<=n;i++)cout<<(char)(b[i]+'A'-1);
		cout<<".";
	}
	else if(fl==2)cout<<"Inconsistency found after "<<i<<" relations.";
	else cout<<"Sorted sequence cannot be determined.";
}
2025/1/18 18:13
加载中...