Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: TwoSAT
(src/math/two_sat.hpp)

TwoSAT

2-SATを解きます.
変数 $x_0, x_1, \cdots, x_{N - 1}$ に関して,

というクローズを足し,これをすべて満たす変数の割当があるかを解きます.

コンストラクタ

TwoSAT ts(int n)

計算量

add_clause

void ts.add_clause(int i, bool f, int j, bool g)

$(x_i = f) \lor (x_j = g)$ というクローズを足します.

制約

計算量

satisfiable

bool ts.satisfiable()

条件を足す割当が存在するかどうかを判定します.
割当が存在するならば true ,そうでないなら false を返します.

複数回呼ぶことも可能です.

計算量

足した制約の個数を $m$ として

answer

vector<bool> ts.answer()

最後に呼んだ satisfiable の,クローズを満たす割当を返します.
satisfiable を呼ぶ前や, satisfiable で割当が存在しなかったときにこの関数を呼ぶと,中身が未定義の長さ $n$ の vectorを返します.

計算量

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"
#include "../graph/graph_template.hpp"
#include "../graph/strongly_connected_components.hpp"
struct TwoSAT {
    TwoSAT(int N)
        : n(N), ans(N), graph(2 * N) {}
    void add_clause(const int i, const bool f, const int j, const bool g) {
        graph.add_directed_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0));
        graph.add_directed_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0));
    }
    bool satisfiable() {
        const vector<int> id = scc_ids(graph).second;
        for(int i = 0; i < n; ++i) {
            if(id[2 * i] == id[2 * i + 1]) return false;
            ans[i] = id[2 * i] < id[2 * i + 1];
        }
        return true;
    }
    vector<bool> answer() const {
        return ans;
    }

   private:
    int n;
    vector<bool> ans;
    Graph<int> graph;
};
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/graph/compressed_sparse_row.hpp"
template <typename T>
struct CompressedSparseRow {
    vector<int> start, elist;
    CompressedSparseRow(const Graph<T>& g)
        : start(g.size() + 1), elist(g.edge_size()) {
        const int n = g.size();
        for(int i = 0; i < n; ++i) {
            start[i + 1] = start[i] + g[i].size();
            int counter = start[i];
            for(const Edge<T>& e : g[i]) {
                elist[counter++] = e.to;
            }
        }
    }
};
#line 5 "src/graph/strongly_connected_components.hpp"
template <typename T>
pair<int, vector<int>> scc_ids(const Graph<T>& g) {
    const int n = g.size();
    const CompressedSparseRow<T> g_csr(g);
    int now_ord = 0, group_num = 0;
    vector<int> visited, low(n), ord(n, -1), ids(n);
    visited.reserve(n);
    auto dfs = [&](const auto& dfs, const int v) -> void {
        low[v] = ord[v] = now_ord++;
        visited.emplace_back(v);
        for(int i = g_csr.start[v]; i < g_csr.start[v + 1]; ++i) {
            const int to = g_csr.elist[i];
            if(ord[to] == -1) {
                dfs(dfs, to);
                low[v] = min(low[v], low[to]);
            } else {
                low[v] = min(low[v], ord[to]);
            }
        }
        if(low[v] == ord[v]) {
            while(true) {
                const int u = visited.back();
                visited.pop_back();
                ord[u] = n;
                ids[u] = group_num;
                if(u == v) break;
            }
            ++group_num;
        }
    };
    for(int i = 0; i < n; ++i) {
        if(ord[i] == -1) {
            dfs(dfs, i);
        }
    }
    for(auto& x : ids) x = group_num - 1 - x;
    return {group_num, ids};
}
template <typename T>
vector<vector<int>> strongly_connected_components(const Graph<T>& g) {
    const auto [group_num, ids] = scc_ids(g);
    const int n = g.size();
    vector<int> counts(group_num);
    for(const int x : ids) ++counts[x];
    vector<vector<int>> groups(group_num);
    for(int i = 0; i < group_num; ++i) groups[i].reserve(counts[i]);
    for(int i = 0; i < n; ++i) groups[ids[i]].emplace_back(i);
    return groups;
}
#line 5 "src/math/two_sat.hpp"
struct TwoSAT {
    TwoSAT(int N)
        : n(N), ans(N), graph(2 * N) {}
    void add_clause(const int i, const bool f, const int j, const bool g) {
        graph.add_directed_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0));
        graph.add_directed_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0));
    }
    bool satisfiable() {
        const vector<int> id = scc_ids(graph).second;
        for(int i = 0; i < n; ++i) {
            if(id[2 * i] == id[2 * i + 1]) return false;
            ans[i] = id[2 * i] < id[2 * i + 1];
        }
        return true;
    }
    vector<bool> answer() const {
        return ans;
    }

   private:
    int n;
    vector<bool> ans;
    Graph<int> graph;
};
Back to top page