WA60求助,悬赏互关
查看原帖
WA60求助,悬赏互关
760018
蟋蟀喵~~STLlover楼主2023/8/15 10:53
#include <bits/stdc++.h>
using namespace std;
vector<int> a[27];
int n,m,du[27],td[27],ff;
char x,y,t;
queue <char> q1,q2;
bool f(){
	for(int i = 0; i < n; i++){
		if(td[i] == 0){
			q1.push(i + 65);
		} 
	}
	bool q = false;
	while(q1.size()){
		if(q1.size() >= 2){
			ff = 0;
		}
		q = true;
		int t = q1.front() - 65;
		q2.push(q1.front());
		q1.pop();
		if(a[t].size() == 0){
			continue;
		}
		for(int i = 0; i < a[t].size(); i++){
			if(td[a[t][i]] == 1){
				q1.push(a[t][i] + 65);
				td[a[t][i]]--;
				q = 0; 
			}else if(td[a[t][i]] > 1){
				td[a[t][i]]--;
				q = 0;
			}
		}
		if(q) return 0;
	}
	if(q)
	return 1;
}
int main(){
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		ff = 1;
		while(q2.size()){
			q2.pop();
		}
		cin >> x >> t >> y;
		if(x == y){
			cout << "Inconsistency found after " << i << " relations.";
			return 0;
		}
		if(t == '>'){
			swap(x,y);
		}
		bool tt = true;
		for(int i = 0; i < a[x - 65].size(); i++){
			if(a[x - 65][i] == y - 65){
				tt = 0;
			}
		}
		if(tt) {
			a[x - 65].push_back(y - 65);
			du[y - 65]++;	
		}
		memcpy(td,du,sizeof(du));
		if(!f()){
			cout << "Inconsistency found after " << i << " relations.";
			return 0;
		}else if(q2.size() == n && ff){
			cout << "Sorted sequence determined after " << i << " relations: ";
			while(q2.size()){
				cout << char(q2.front());
				q2.pop();
			}
			cout << '.';
			return 0;
		}
	}
	cout << "Sorted sequence cannot be determined.";
	return 0;
}

2023/8/15 10:53
加载中...