80pts求助
查看原帖
80pts求助
377969
george0929楼主2024/8/22 15:20
#include<bits/stdc++.h>
using namespace std;
vector<int> V[200005],reV[200005];
int vis[200005],id[200005],cnt;
stack<int> stk;
void dfs1(int u){
	for(int v:V[u]){
		if(vis[v]) continue;
		vis[v]=1;
		dfs1(v);
	}
	stk.push(u);
}
void dfs2(int u){
	for(int v:reV[u]){
		if(id[v]) continue;
		id[v]=cnt;
		dfs2(v);
	}
}
int n,d,m;
int S[200005];
int x[200005],a[200005],y[200005],b[200005];
int p(int t1,int t2){
	if(S[t1]==1){
		if(t2==2) return t1;
		return t1+n;
	}
	if(S[t1]==2){
		if(t2==1) return t1;
		return t1+n;
	}
	if(S[t1]==3){
		if(t2==1) return t1;
		return t1+n;
	}
}
int pr(int t1,int t2){
	if(S[t1]==1){
		if(t2==2) return t1+n;
		return t1;
	}
	if(S[t1]==2){
		if(t2==1) return t1+n;
		return t1;
	}
	if(S[t1]==3){
		if(t2==1) return t1+n;
		return t1;
	}
}
void add(int u,int v){
	V[u].push_back(v);
	reV[v].push_back(u);
//	cout<<u<<" "<<v<<endl;
}
void solve(){
	cnt=0;
	for(int i=1;i<=2*n;i++) V[i].clear(),reV[i].clear();
	memset(vis,0,sizeof(vis));
	memset(id,0,sizeof(id));
	for(int t=1;t<=m;t++){
		if(a[t]!=S[x[t]]&&b[t]!=S[y[t]]){
			int p1=p(x[t],a[t]),p2=p(y[t],b[t]);
			add(p1,p2);
			p1=pr(x[t],a[t]),p2=pr(y[t],b[t]);
			add(p2,p1);
		}else if(a[t]!=S[x[t]]){
			add(p(x[t],a[t]),pr(x[t],a[t]));
		}
	}
	for(int i=1;i<=2*n;i++){
		if(!vis[i]){
			vis[i]=1;
			dfs1(i);
		}
	}
	while(!stk.empty()){
		int u=stk.top();
		stk.pop();
		if(id[u]) continue;
		id[u]=++cnt;
		dfs2(u);
	}
	for(int i=1;i<=n;i++){
		if(id[i]==id[i+n]){
			return;
		}
	}
	for(int i=1;i<=n;i++){
		if(id[i]>id[i+n]){
			if(S[i]==1){
				cout<<'B';
			}else{
				cout<<'A';
			}
		}else{
			if(S[i]==3){
				cout<<'B';
			}else{
				cout<<'C';
			}
		}
	}
	exit(0);
}
vector<int> pos;
int main(){
	freopen("P3825_16.in","r",stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>d;
	string s;
	cin>>s;
	for(int i=0;i<s.length();i++){
		s[i]=toupper(s[i]);
		if(s[i]=='X'){
			S[i+1]=0;
			pos.push_back(i+1);
		}else{
			S[i+1]=s[i]-'A'+1;
		}
	}
	cin>>m;
	for(int t=1;t<=m;t++){
		char t1,t2;
		cin>>x[t]>>t1>>y[t]>>t2;
		t1=toupper(t1),t2=toupper(t2);
		a[t]=t1-'A'+1;
		b[t]=t2-'A'+1;
	}
	for(int i=0;i<(1<<d);i++){
		for(int j=0;j<pos.size();j++){
			if(j&(1<<i)){
				S[pos[j]]=1;
			}else{
				S[pos[j]]=2;
			}
		}
		solve();
	}
	cout<<-1<<endl;
}
2024/8/22 15:20
加载中...