luogu P3825 [NOI2017] 游戏
题面传送门
如果没有x就是2-SAT裸题。
具体的,如果当前\(x\)不满足,直接跳过,如果\(y\)不满足,那么连边如果\(x=nx\)满足,那么\(x=nx^1\)满足。其它就正常连边。
然而这题有x。
考虑到x的数量很少,我们可以直接爆搜每个x是\(a\)还是\(b\)还是\(c\),但是这样不优。
发现其实\(a\)与\(b\)就囊括了全部的情况所以可以直接搜这两个,复杂度\(O(n2^{d})\)
code:
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#define I inline
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define re register
#define ll long long
#define db double
#define N 100000
#define M 20
#define mod 1000000007
#define eps (1e-7)
#define U unsigned int
#define IT set<ques>::iterator
#define Gc() getchar()
I void read(int &x){
char s=getchar();x=0;while(s<'0'||s>'9') s=Gc();
while(s>='0'&&s<='9') x=x*10+s-48,s=Gc();
}
using namespace std;
int n,m,A[N+5],B[N+5],Bh,x[N+5],y[N+5],nx[N+5],ny[N+5],cnt,scc[N+5],dh,dfn[N+5],vis[N+5],low[N+5],st[N+5],sh,G[N+5][4];char C[N+5],S;
struct yyy{int to,z;};
struct ljb{int head,h[N+5];yyy f[N+5<<1];I void add(int x,int y){f[++head]=(yyy){y,h[x]};h[x]=head;}}s;
I void tarjan(int x){
dfn[x]=low[x]=++dh;vis[x]=1;st[++sh]=x;yyy tmp;for(int i=s.h[x];i;i=tmp.z){
tmp=s.f[i];if(!dfn[tmp.to]) tarjan(tmp.to),low[x]=min(low[x],low[tmp.to]);
else vis[tmp.to]&&(low[x]=min(low[x],low[tmp.to]));
}
if(low[x]==dfn[x]){++cnt;while(st[sh+1]^x) scc[st[sh]]=cnt,vis[st[sh--]]=0;}
}
I void solve(){
re int i;/*printf("%d\n",m);*/memset(s.h,0,sizeof(s.h));s.head=0;for(i=1;i<=n;i++)G[i][1]=(A[i]>1);
for(i=1;i<=m;i++){
if(A[x[i]]==nx[i]) continue;if(A[y[i]]==ny[i]) s.add(x[i]+G[x[i]][nx[i]]*n,x[i]+(G[x[i]][nx[i]]^1)*n);
else s.add(x[i]+G[x[i]][nx[i]]*n,y[i]+G[y[i]][ny[i]]*n),s.add(y[i]+(G[y[i]][ny[i]]^1)*n,x[i]+(G[x[i]][nx[i]]^1)*n);
}
dh=cnt=0;memset(dfn,0,sizeof(dfn));for(i=1;i<=2*n;i++) !dfn[i]&&(tarjan(i),0);
for(i=1;i<=n;i++) if(scc[i]==scc[i+n]) return;for(i=1;i<=n;i++)putchar(!A[i]?(scc[i]<scc[i+n]?'B':'C'):(A[i]==1?(scc[i]<scc[i+n]?'A':'C'):(scc[i]<scc[i+n]?'A':'B')));exit(0);
}
I void dfs(int x){if(x==Bh+1)return solve();A[B[x]]=0;dfs(x+1);A[B[x]]=1;dfs(x+1);}
int main(){
freopen("1.in","r",stdin);freopen("1.out","w",stdout);
re int i;read(n);read(m);scanf("%s",C+1);read(m);for(i=1;i<=n;i++) A[i]=C[i]-'a',(A[i]>2)&&(B[++Bh]=i),G[i][2]=1;
for(i=1;i<=m;i++) read(x[i]),nx[i]=Gc()-'A',read(y[i]),ny[i]=Gc()-'A';dfs(1);printf("-1\n");return 0;
}