2017/4/15 Twosat介绍

本文详细介绍了Twosat算法的基本原理,并提供了C++实现的代码模板。通过实例讲解了如何利用Twosat解决实际问题,同时推荐了几道相关的练习题目供读者实践。

Twosat讲解详见算法入门经典1 324页

贴出白书模板

struct TwoSAT{
    int n;
    vector <int> G[maxn*2];
    bool mark[maxn*2];
    int S[maxn*2], c;
    bool dfs(int x){
        if(mark[x^1]) return false;
        if(mark[x]) return true;
        mark[x] = true;
        S[c++] = x;
        for(int i = 0; i < G[x].size(); i++){
            if(!dfs(G[x][i])) return false;
        }
        return true;
    }
    void init(int n){
        this->n = n;
        for(int i = 0; i < n*2; i++) G[i].clear();
        memset(mark, 0, sizeof(mark));
    }
    void addedge(int x, int xval, int y, int yval){
        x = x * 2 + xval;
        y = y * 2 + yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }
    bool solve(){
        for(int i = 0; i < 2*n; i += 2)
        if(!mark[i] && !mark[i+1]){
            c = 0;
            if(!dfs(i)){//已经尝试过把i标记为true,然而不行,那么在尝试将i+1标记为真时,先把i的影响消掉
                while(c > 0) mark[S[--c]] = false;
                if(!dfs(i + 1)) return false;
            }
        }
        return true;
    }
}twosat;

例题推荐:

UVALIVE 3211 二分+TwoSAT

UVALIVE 3713 宇航员分组 TWOSAT

CF 766D TWOSAT http://blog.youkuaiyun.com/just_sort/article/details/60763945

#include <bits/stdc++.h> // #include "atcoder/convolution" // #include "atcoder/dsu" // #include "atcoder/fenwicktree" // #include "atcoder/lazysegtree" // #include "atcoder/math" // #include "atcoder/maxflow" // #include "atcoder/mincostflow" // #include "atcoder/modint" // #include "atcoder/scc" // #include "atcoder/segtree" // #include "atcoder/string" // #include "atcoder/twosat" // using namespace __gnu_cxx; // using namespace __gnu_pbds; using namespace std; auto rng = mt19937(random_device()()); auto rngl = mt19937_64(random_device()()); // Let's set a bit and flow! // I came, I divided, I conquered! int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t --) { int n, q; cin >> n >> q; vector<long long> nums(n); for (auto &x: nums) cin >> x; vector<long long> vis = nums, cur = nums; vector<pair<int, int>> updates(q); for (auto &[idx, val]: updates) { cin >> idx >> val; idx --; cur[idx] += val; vis.emplace_back(cur[idx]); } sort(vis.begin(), vis.end()); vis.erase(unique(vis.begin(), vis.end()), vis.end()); int k = vis.size(); vector<int> fen(k + 1, 0); auto getidx = [&] (long long val) -> int { return lower_bound(vis.begin(), vis.end(), val) - vis.begin(); }; auto add = [&] (int idx, int val) -> void { idx ++; while (idx <= k) { fen[idx] += val; idx += idx & -idx; } }; auto not_exceed = [&] (int val) -> int { int res = 0, idx = 0; for (int i = 20; i >= 0; i --) { if ((idx | (1 << i)) <= k && res + fen[idx | (1 << i)] <= val) { idx |= 1 << i; res += fen[idx]; } } return res; }; for (
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值