2-SAT

· · 个人记录

解释名词:SAT 是 Satisfiability(可满足性/适定性)问题的简写

N-SAT 问题指有 n 个 bool 变量,求解 n 元布尔方程组

当 n \ge 3 时,问题已被证明为 NP 完全的,暂时没有多项式解法

但 n = 2 时问题存在线性时间解法,即 2-SAT

模板:

// Problem: P4782 【模板】2-SAT
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4782
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define int long long 
#define rep(i,a,b)  for(int i = (a);i<=(b);i++)
#define lep(i,a,b)  for(int i = (a);i>=(b);i--)
#define gep(v,u)    for(auto v:g[u])
using namespace std;
const int N = 4e6+10;
int dfn[N],low[N],sc[N];
stack<int> st;
vector<int> g[N<<1];
int n,m,cnt,cnt1,cnt2,a,b,va,vb,na,nb;
void dfs(int u){
    low[u] = dfn[u] = ++cnt;
    st.push(u);
    gep(v,u){
        if(!dfn[v]) dfs(v),low[u] = min(low[u],low[v]);
        else if(!sc[v]) low[u] = min(low[u],dfn[v]);
    }
    if(low[u] == dfn[u]){
        cnt1++; 
        while(1){
            int v = st.top();
            st.pop();
            sc[v] = cnt1;
            if(u == v) break;
        }
    }
}
bool tarjan(){
    rep(i,1,2*n) if(dfn[i] == 0) dfs(i);
    rep(i,1,n) if(sc[i] == sc[i+n]) return 0;
    return 1;
}
signed main(){
    scanf("%lld%lld",&n,&m);
    rep(i,1,m){
        scanf("%lld%lld%lld%lld",&a,&va,&b,&vb);
        na = va^1; nb = vb^1;
        g[a+na*n].push_back(b+vb*n);
        g[b+nb*n].push_back(a+va*n);
    }
    if(tarjan()){
        printf("POSSIBLE\n");
        rep(i,1,n)  printf("%lld ",(int)sc[i]>sc[i+n]);
    }
    else    printf("IMPOSSIBLE");
    return 0;
}

变式

题面

// Problem: P4171 [JSOI2010] 满汉全席
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4171
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define int long long 
#define rep(i,a,b)  for(int i = (a);i<=(b);i++)
using namespace std;
const int N = 4e6+10;
struct node{int to,net;}ed[N];
int h[N],dfn[N],low[N],sc[N];
stack<int> s;
int n,m,a,b,t,cnt1,cnt,cnt2;
char c1,c2;
string s1,s2;
void init(){
    cnt = cnt1 = cnt2 = 0;
    for(int i = 1;i<=2*n+10;i++) h[i] = 0,sc[i] = 0,low[i] = 0,dfn[i] = 0;
    while(s.size()) s.pop();
}
void add(int u,int v){
    ed[++cnt2].to = v;
    ed[cnt2].net = h[u];
    h[u] = cnt2;
}
void dfs(int u){
    low[u] = dfn[u] = ++cnt;
    s.push(u);
    for(int i = h[u];i;i = ed[i].net){
        int v = ed[i].to;
        if(!dfn[v]){
            dfs(v);
            low[u] = min(low[u],low[v]);
        }
        else if(!sc[v]){
            low[u] = min(low[u],dfn[v]);
        }
    }
    if(low[u] == dfn[u]){
        cnt1++; 
        while(1){
            int v = s.top();
            s.pop();
            sc[v] = cnt1;
            if(u == v){
                break;
            }
        }
    }
}
bool tarjan(){
    rep(i,1,n<<1) if(dfn[i] == 0) dfs(i);
    rep(i,1,n) if(sc[i] == sc[i+n]) return 0;
    return 1;
}
signed main(){
    cin>>t;
    while(t--){
        cin>>n>>m;  init();
        rep(i,1,m){
            cin>>s1>>s2;
            c1 = s1[0]; c2 = s2[0]; a = b = 0;
            rep(j,1,(int)s1.size()-1)   a = a*10+s1[j]-'0';
            rep(j,1,(int)s2.size()-1)   b = b*10+s2[j]-'0';
            if(c1 == 'h' && c2 == 'h')  add(a+n,b),add(b+n,a);
            if(c1 == 'h' && c2 == 'm')  add(a+n,b+n),add(b,a);
            if(c1 == 'm' && c2 == 'm')  add(a,b+n),add(b,a+n);
            if(c1 == 'm' && c2 == 'h')  add(a,b),add(b+n,a+n); 
        }
        if(tarjan())    cout<<"GOOD"<<endl;
        else    cout<<"BAD"<<endl;
    }
    return 0;
}

CF 1971 H

题面

给一个 3*n 的矩阵,矩阵的值为 \pm a[i],(1\le i \le n),你可以确定数组 a (\forall a[i] = \pm1 ) 的值,并填入矩阵,接着对矩阵每一列从小到大排序,求是否可以使中间一行都为 1 。

解:每个数仅有两个取值,考虑 2-SAT ,对于每一列满足布尔方程 (x \vee y) \wedge (y \vee z) \wedge (x \vee z) = true (这是2-SAT的一类常见形式),板子。

// Problem: H. ±1
// Contest: Codeforces - Codeforces Round 944 (Div. 4)
// URL: https://codeforces.com/contest/1971/problem/H
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b)  for(int i = (a);i<=(b);i++)
#define lep(i,a,b)  for(int i = (a);i>=(b);i--)
#define gep(v,u)    for(auto v:g[u])
using namespace std;
const int N = 6006;
int dfn[N],low[N],sc[N];
stack<int> s;
int t,n,m,p[4][N],a,b,va,vb,na,nb;
int cnt1,cnt,cnt2;
bool ans;
vector<int> g[N];
inline void dfs(int u){
    low[u] = dfn[u] = ++cnt;
    s.push(u);
    gep(v,u){
        if(!dfn[v]) dfs(v),low[u] = min(low[u],low[v]);
        else if(!sc[v]) low[u] = min(low[u],dfn[v]);
    }
    if(low[u] == dfn[u]){
        cnt1++; 
        while(1){
            int v = s.top(); s.pop();
            sc[v] = cnt1;
            if(u == v)  break;
        }
    }
}
inline bool tarjan(){
    rep(i,1,2*n) if(dfn[i] == 0) dfs(i);
    rep(i,1,n) if(sc[i] == sc[i+n]) return 0;
    return 1;
}
inline void solve(){
    scanf("%lld",&m);
    n = m*3;
    rep(i,1,3)  rep(j,1,m)  scanf("%lld",&p[i][j]);
    rep(i,1,n<<1)   g[i].clear(),low[i] = dfn[i] = 0,sc[i] = 0;
    rep(i,1,m){
        rep(j,1,3){
            rep(k,j+1,3){
                a = abs(p[j][i]);
                b = abs(p[k][i]);
                va = p[j][i] >= 1?1:0;
                vb = p[k][i] >= 1?1:0;
                na = va^1;
                nb = vb^1;
                g[a+na*n].push_back(b+vb*n);
                g[b+nb*n].push_back(a+va*n);
            }
        }
    }
    ans = tarjan();
    printf("%s\n",ans?"YES":"NO");
}
signed main(){
    scanf("%lld",&t);
    while(t--)  solve();
    return 0;
}