模板 - 2 - SAT问题

整理的算法模板合集: ACM模板


注意一个坑,2SAT问题中如果要求你输出方案,如果你的代码输出的跟样例不一样,不要着急,因为2SAT 问题本来就是有多解,结果我样例不过,交上去就A了

方案输出时,color[x]谁小选谁

有 n 个布尔变量 x 1 ∼ x n x_1\sim x_n x1xn​,另有 m 个需要满足的条件,每个条件的形式都是 「x_i为 true / false 或 x_j​为 true / false」。比如 「x_1为真或 x_3为假」、「x_7​为假或 x_2为假」。2-SAT 问题的目标是给每个变量赋值使得所有条件得到满足。

2 - SAT模板

//时间复杂度O(n+m)
//当 x 所在的强连通分量的拓扑序在 ¬x 所在的强连通分量的拓扑序之后取 x
//为真 ,注意我们得到的是拓扑逆序,所以要写成color[x] < color[¬x] 
// 其中 i 表示 ¬x,用 i+n 表示 x
p ∨ q == ¬p → q  ∧ ¬q → p 
p || q == -p ->q && -q -> p
typedef long long ll;

const int N = 2000007, M = 5000007, INF = 0x3f3f3f3f;

int n, m;
int dfn[N], low[N], num;
bool vis[N], ins[N];
int a[N], scc_cnt;
int scc_id[N], color[N];
int stk[N], top;
int ver[M], nex[M], head[N], tot;

void add(int x, int y){
    
    
    ver[tot] = y;
    nex[tot] = head[x];
    head[x] = tot ++ ;
}

void tarjan(int x)
{
    
    
    dfn[x] = low[x] = ++ num;
    stk[++ top] = x;
    ins[x] = true;
    for(int i = head[x]; ~i; i = nex[i]){
    
    
        int y = ver[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]){
    
    
        int y;
        ++ scc_cnt;
        do{
    
    
            y = stk[top -- ];
            ins[y] = false;
            scc_id[y] = scc_cnt;
            color[y] = scc_cnt;
        }while(x != y);
    }
}

int main()
{
    
    
    memset(head, -1, sizeof head);
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; ++ i){
    
    
        int p, q ,c ,d;
        scanf("%d%d%d%d", &p, &c, &q, &d);
        if(c && d){
    
    // p 且 q  -p-> q && -q -> p
            add(p, q + n);
            add(q, p + n);
        }
        else if(!c && d){
    
    //p -> q && -q -> -p
            add(p + n, q + n);
            add(q, p);
        }
        else if(c && !d){
    
    //-p -> -q && q -> p
            add(p, q);
            add(q + n, p + n);
        }
        else if(!c && !d){
    
    //p -> -q && q -> -p
            add(p + n, q);
            add(q + n, p);
        }
    }
    for(int i = 1; i <= 2 * n; ++ i){
    
    
        if(!dfn[i])
            tarjan(i);
    }

    for(int i = 1 ;i <= n; ++ i) {
    
    
        if(color[i] == color[i + n]){
    
    
            puts("IMPOSSIBLE");
            return 0;
        }
    }
    puts("POSSIBLE");
    //谁小选谁,这里i + n表示的是x (true)
    for(int i = 1; i <= n; ++ i)
        printf("%d ", (color[i] > color[i + n]));
    puts("");
    return 0;
}

位运算版

	g[p + n * c].push_back(q + n * (d ^ 1));
    g[q + n * d].push_back(p + n * (c ^ 1));

需要注意的是,如果题目中有重边的话,使用链式前向星就会导致RE,改成vector即可
如下题

2 - SAT + 二分答案


/*ACM-ICPC 2004 Europe - Southwestern) Now or later*/
typedef long long ll;
const int N = 5000007, M = 5000007, INF = 0x3f3f3f3f;

int n, m;
int dfn[N], low[N], num;
int head[N], ver[M], nex[M], tot;
vector<int>g[N];
int a[N][2];
int stk[N], top, scc_cnt;
bool ins[N];
int color[N];

void tarjan(int x)
{
    
    
    dfn[x] = low[x] = ++ num;
    stk[++ top] = x;
    ins[x] = true;
    for(size_t i = 0; i < g[x].size(); ++ i){
    
    
        int y = g[x][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]){
    
    
        int y;
        ++ scc_cnt;
        color[x] = scc_cnt;
        do{
    
    
            y = stk[top -- ];
            ins[y] = false;
            color[y] = scc_cnt;
        }while(x != y);
    }
    return ;
}

inline bool check(int x){
    
    
    //memset(head, -1, sizeof head);
    for(int i = 1; i <= 2 * n; ++ i)
        g[i].clear();
    memset(dfn, 0, sizeof dfn);
    memset(low, 0, sizeof low);
    memset(color, 0, sizeof color);
    memset(ins, 0, sizeof ins);
    tot = num = scc_cnt = 0;

    //0 : -x : E 早 : i
    //1 : x : L 晚 : i + n

    //比较之后的两个相差的时间是否小于x
    //如果小于x的话那么两者就不能同时出现(同时为true)
    for(int p = 1;p <= n;++ p){
    
    
        for(int c = 0; c <= 1; ++ c){
    
    
            for(int q = p + 1; q <= n; ++ q){
    
    
                for(int d = 0; d <= 1; ++ d){
    
    
                    if(abs(a[p][c] - a[q][d]) < x){
    
    
                        /*if(c && d){//11 -> 01, 01
                            add(p, q + n);
                            add(q, p + n);
                        }
                        else if(!c && d){//01 -> 11, 00
                            add(p + n, q + n);
                            add(q, p);
                        }
                        else if(c && !d){//10 -> 00, 11
                            add(p, q);
                            add(q + n, p + n);
                        }
                        else if(!c && !d){//00 -> 10, 01
                            add(p + n, q);
                            add(q + n, p);
                        }*/
                        g[p + n * c].push_back(q + n * (d ^ 1));
                        g[q + n * d].push_back(p + n * (c ^ 1));
                    }
                }
            }
        }
    }

    for(int i = 1; i <= 2 * n; ++ i)
        if(!dfn[i])
            tarjan(i);

    for(int i = 1; i <= n; ++ i){
    
    
        if(color[i] == color[i + n])
            return false;
    }
    return true;
}

void solve(){
    
    
    int l = 0, r = 0, ans = -1;

    for(int i =1; i <= n; ++ i)
            scanf("%d%d", &a[i][0], &a[i][1]), r = max(max(a[i][1], a[i][0]), r);


    while(l <= r){
    
    
        int mid = l + r >> 1;
        if(check(mid))ans = mid, l = mid + 1;
        else r = mid - 1;
    }
    printf("%d\n", ans);
    return ;
}

int main()
{
    
    
    while(scanf("%d", &n) != EOF){
    
    
        solve();
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_45697774/article/details/108712378
今日推荐