孙江昊

BALENCIAGA  •  4个月前


include

include

include

include

include

define cls(s) memset(s,0,sizeof(s))

define LL long long int

define REP(i,n) for (int i = 1; i <= (n); i++)

define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)

define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");

using namespace std; const int maxn = 100005,maxm = 200005,INF = 1000000000; inline int read(){

int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;

} int h[maxn],ne = 1; struct EDGE{int to,nxt;}ed[maxm]; inline void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;} const char alpha[] = {"abc"}; char s[maxn],P[maxn],A[maxn],B[maxn]; int pos[20],n,d,m,p1[maxn],p2[maxn]; int Scc[maxn],dfn[maxn],low[maxn],st[maxn],top,cnt,scci; inline int id(int u,char c){

if (c == 'a') return u;
if (c == 'b' && P[u] == 'a') return u;
return u + n;

} inline char Get(int u,int v){

if (P[u] == 'a') return v ? 'B' : 'C';
if (P[u] == 'b') return v ? 'A' : 'C';
return v ? 'A' : 'B';

} void dfs(int u){

dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
    if (!dfn[to = ed[k].to]){
        dfs(to);
        low[u] = min(low[u],low[to]);
    }else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
    scci++;
    do{
        Scc[st[top]] = scci;
    }while (st[top--] != u);
}

} bool solve(){

for (int i = 1; i <= (n << 1); i++)
    h[i] = Scc[i] = dfn[i] = low[i] = 0;
top = cnt = scci = 0; ne = 1;
for (int i = 1; i <= m; i++){
    int a = p1[i],b = p2[i];
    int x = id(a,A[i]),y = id(b,B[i]);
    int x1 = x > n ? x - n : x + n,y1 = y > n ? y - n : y + n;
    if (A[i] == P[a]) continue;
    if (B[i] == P[b]) build(x,x1);
    else build(x,y),build(y1,x1);
}
for (int i = 1; i <= (n << 1); i++) if (!dfn[i]) dfs(i);
for (int i = 1; i <= n; i++) if (Scc[i] == Scc[i + n]) return false;
/*puts("");
for (int i = 1; i <= (n << 1); i++) printf("%d:%d\n",i,Scc[i]); puts("");*/

for (int i = 1; i <= n; i++) putchar(Get(i,Scc[i] < Scc[i + n]));
return true;

} bool Dfs(int u){

if (u > d) return solve();
for (int i = 0; i < 2; i++){
    P[pos[u]] = alpha[i];
    if (Dfs(u + 1)) return true;
}
return false;

} int main(){

n = read(); d = read(); d = 0;
scanf("%s",s + 1);
for (int i = 1; i <= n; i++)
    if (s[i] == 'x') pos[++d] = i;
    else P[i] = s[i];
m = read();
for (int i = 1; i <= m; i++){
    scanf("%d %c %d %c",&p1[i],&A[i],&p2[i],&B[i]);
    A[i] += 'a' - 'A'; B[i] += 'a' - 'A';
}
if (!Dfs(1)) puts("-1");
return 0;

}


评论:

请先登录,才能进行评论