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: verify/library_checker/other/2_sat.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#include "../../../src/template/template.hpp"
#include "../../../src/math/two_sat.hpp"
int main(void) {
    string p, cnf;
    int n, m;
    cin >> p >> cnf >> n >> m;
    TwoSAT ts(n);
    rep(i, 0, m) {
        int a, b, z;
        cin >> a >> b >> z;
        bool f = a > 0, g = b > 0;
        a = abs(a) - 1;
        b = abs(b) - 1;
        ts.add_clause(a, f, b, g);
    }
    if(ts.satisfiable()) {
        cout << "s SATISFIABLE" << '\n';
        vector<bool> ans = ts.answer();
        cout << "v ";
        rep(i, 0, n) {
            if(!ans[i]) cout << '-';
            cout << i + 1 << ' ';
        }
        cout << 0 << '\n';
    } else {
        cout << "s UNSATISFIABLE" << '\n';
    }
}
#line 1 "verify/library_checker/other/2_sat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#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;
};
#line 4 "verify/library_checker/other/2_sat.test.cpp"
int main(void) {
    string p, cnf;
    int n, m;
    cin >> p >> cnf >> n >> m;
    TwoSAT ts(n);
    rep(i, 0, m) {
        int a, b, z;
        cin >> a >> b >> z;
        bool f = a > 0, g = b > 0;
        a = abs(a) - 1;
        b = abs(b) - 1;
        ts.add_clause(a, f, b, g);
    }
    if(ts.satisfiable()) {
        cout << "s SATISFIABLE" << '\n';
        vector<bool> ans = ts.answer();
        cout << "v ";
        rep(i, 0, n) {
            if(!ans[i]) cout << '-';
            cout << i + 1 << ' ';
        }
        cout << 0 << '\n';
    } else {
        cout << "s UNSATISFIABLE" << '\n';
    }
}
Back to top page