第一眼看上去似乎是一个3-SAT问题
然而\(d \leq 8\)给我们的信息就是:暴力枚举
枚举\(x\)型地图变成\(a\)型地图还是\(b\)型地图(实际上不要枚举\(c\),因为\(ab\)两种地图已经包含了选择\(ABC\)三辆车的情况),对于每一种情况跑2-SAT即可。复杂度\(O(2^d(n+m))\)
还有为什么UOJ的Hack那么强啊QAQ
随机化要么WA EX5要么WA EX8,不随机化TLE EX9
UPD:在Itst的重构之后终于过了UOJ的Hack,只是不知道为什么数组一定要开\(10^5\)
#include<bits/stdc++.h>
//this code is written by Itst
using namespace std;
int read(){
int a = 0;
char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return a;
}
char getc(){
char c = getchar();
while(!isupper(c)) c = getchar();
return c;
}
const int MAXN = 1e5 + 3;
struct Edge{
int end , upEd;
}Ed[MAXN << 2];
struct que{
int a , tpa , b , tpb;
}now[MAXN << 1];
char s[MAXN];
int dfn[MAXN << 1] , low[MAXN << 1] , in[MAXN << 1] , head[MAXN << 1];
int N , M , D , ts , cntEd , cntSCC , X[9];
int stk[MAXN << 1] , top;
bool vis[MAXN << 1] , ins[MAXN << 1];
void addEd(int a , int b){
Ed[++cntEd] = (Edge){b , head[a]};
head[a] = cntEd;
}
void clear(){
memset(head , 0 , sizeof(head));
memset(vis , 0 , sizeof(vis));
memset(ins , 0 , sizeof(ins));
memset(in , 0 , sizeof(in));
cntSCC = cntEd = ts = top = 0;
}
bool pop(int tar){
++cntSCC;
do{
int t = stk[top];
in[t] = cntSCC;
if(in[t > N ? t - N : t + N] == in[t])
return 0;
}while(stk[top--] != tar);
return 1;
}
int ind(int x , int p){
if(s[x] != 'c') return (p == 2) * N + x;
return (p == 1) * N + x;
}
bool tarjan(int x){
vis[x] = ins[x] = 1;
dfn[x] = low[x] = ++ts;
stk[++top] = x;
for(int i = head[x] ; i ; i = Ed[i].upEd){
if(!vis[Ed[i].end]){
if(!tarjan(Ed[i].end)) return 0;
}
else if(!ins[Ed[i].end]) continue;
low[x] = min(low[x] , low[Ed[i].end]);
}
ins[x] = 0;
if(dfn[x] == low[x])
return pop(x);
return 1;
}
bool work(){
for(int i = 1 ; i <= M ; ++i){
bool f1 = now[i].tpa == s[now[i].a] - 'a' , f2 = now[i].tpb == s[now[i].b] - 'a';
if(f1) continue;
if(f2){
int pos = ind(now[i].a , now[i].tpa);
addEd(pos , now[i].a * 2 + N - pos);
}
int posA = ind(now[i].a , now[i].tpa) , posB = ind(now[i].b , now[i].tpb);
addEd(posA , posB); addEd(now[i].b * 2 + N - posB , now[i].a * 2 + N - posA);
}
for(int i = 1 ; i <= 2 * N ; ++i)
if(!vis[i] && !tarjan(i)) return 0;
return 1;
}
void output(){
for(int i = 1 ; i <= N ; ++i)
if(in[i] > in[i + N])
putchar(s[i] == 'c' ? 'B' : 'C');
else putchar(s[i] == 'a' ? 'B' : 'A');
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
N = read(); read();
scanf("%s" , s + 1);
for(int i = 1 ; i <= N ; ++i)
if(s[i] == 'x')
X[++D] = i;
M = read();
for(int i = 1 ; i <= M ; ++i){
now[i].a = read(); now[i].tpa = getc() - 'A';
now[i].b = read(); now[i].tpb = getc() - 'A';
}
for(int i = 0 ; i < 1 << D ; ++i){
for(int j = 1 ; j <= D ; ++j)
s[X[j]] = (i >> (j - 1) & 1) + 'a';
if(work()){
output();
return 0;
}
clear();
}
puts("-1");
return 0;
}