悬赏关注 萌新求助 WA 85pts
查看原帖
悬赏关注 萌新求助 WA 85pts
349928
会思考的猪楼主2023/3/23 16:12

rt.

#include<bits/stdc++.h>
using namespace std;

int read(){
	int s=0,w=1; char c=getchar();
	while(!isdigit(c)){ if(c=='-') w=-1; c=getchar();}
	while(isdigit(c)){ s=(s<<3)+(s<<1)+(c^48); c=getchar();}
	return s*w;
}

const int N=2e5+5;

int n,m,d;
char s[N];
vector<int> ve;

struct node{
	int i,j;
	char x,y;
}q[N];

int e,to[N<<1],ne[N<<1],h[N];
void add(int x,int y){
	to[++e]=y,ne[e]=h[x],h[x]=e;
}

int ts,cnt,top;
int dfn[N],low[N];
int id[N],stk[N];
bool ins[N];

void tarjan(int x){
	dfn[x]=low[x]=++ts;
	stk[++top]=x;
	ins[x]=1;
	for(int i=h[x];i;i=ne[i]){
		int y=to[i];
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(ins[y]) low[x]=min(low[x],dfn[y]);
	}
	if(low[x]==dfn[x]){
		++cnt;
		int y;
		do{
			y=stk[top--];
			ins[y]=0;
			id[y]=cnt;
		} while(x!=y);
	}
}

int code(int x,char a,int opt){
	char aa=s[x]-'a';
	a-='A';
	if(((aa+1)%3!=a)^opt) return x+n;
	return x;
}
char decode(char a,int opt){
	int tmp=a-'a';
	return (char)((tmp+opt+1)%3+'A');
}

bool work(){
	memset(h,0,sizeof h);
	memset(low,0,sizeof low);
	memset(dfn,0,sizeof dfn);
	memset(ins,0,sizeof ins);
	e=ts=cnt=top=0;
	
	for(int i=1;i<=m;++i){
		auto &t=q[i];
		int x=t.i,y=t.j;
		char a=t.x,b=t.y;
		if(a-'A'!=s[x]-'a'){
			if(b-'A'!=s[y]-'a') add(code(x,a,0),code(y,b,0)),add(code(y,b,1),code(x,a,1));
			else add(code(x,a,0),code(x,a,1));
		}
	}
	
	for(int i=1;i<=n<<1;++i){
		if(!dfn[i]){
			tarjan(i);
		}
	}
	
	for(int i=1;i<=n;++i){
		if(id[i]==id[i+n]) return 0;
	}
	
	for(int i=1;i<=n;++i){
		if(id[i]<id[i+n]){
			printf("%c",decode(s[i],0));
		}
		else{
			printf("%c",decode(s[i],1));
		}
	}
	
	return 1;
}

signed main(){
	n=read(),d=read();
	scanf("%s",s+1);
	for(int i=1;i<=n;++i){
		if(s[i]=='x') ve.push_back(i);
	}
	m=read();
	for(int i=1;i<=m;++i) scanf("%d %c %d %c",&q[i].i,&q[i].x,&q[i].j,&q[i].y);
	
	int flagg=0;
	for(int tmp=0;tmp<1<<d;++tmp){
		for(int i=0;i<d;++i){
			if(tmp&(1<<i)){
				s[ve[i+1]]='a';
			}
			else{
				s[ve[i+1]]='b';
			}
		}
		int flag=work();
		if(flag){
			flagg=1;
			return 0;
		}
	}
	
	if(!flagg){
		puts("-1");
		return 0;
	}
	
	return 0;
}
2023/3/23 16:12
加载中...