library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_twosat.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#include "../graph/twosat.cpp"

int main() {
    string s, t;
    int n, m;
    cin >> s >> t >> n >> m;
    TwoSAT G(n);
    for (int i = 0; i < m; ++i) {
        int a, b; char x;
        scanf(" %d %d %c", &a, &b, &x);
        if(a < 0) a = n-a;
        if(b < 0) b = n-b;
        G.add_or(a-1, b-1);
    }
    auto ans = G.build();
    if(ans.empty()){
        puts("s UNSATISFIABLE");
    }else {
        puts("s SATISFIABLE");
        printf("v ");
        for (int i = 0; i < ans.size(); ++i) {
            if(ans[i]) printf("%d ", i+1);
            else printf("%d ", -(i+1));
        }
        puts("0");
    }
    return 0;
}
#line 1 "test/yosupo_twosat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#line 1 "graph/twosat.cpp"
struct SCC {
    vector<vector<int>> G, G_r, G_out;
    vector<int> vs, used, cmp;
    SCC() = default;
    explicit SCC(int n) : G(n), G_r(n), used(n), cmp(n) {}

    void add_edge(int a, int b){
        G[a].emplace_back(b);
        G_r[b].emplace_back(a);
    }

    void dfs(int v){
        used[v] = 1;
        for (auto &&u : G[v]) if(!used[u]) dfs(u);
        vs.emplace_back(v);
    }

    void dfs_r(int v, int k){
        used[v] = 1;
        cmp[v] = k;
        for (auto &&u : G_r[v]) if(!used[u]) dfs_r(u, k);
    }

    int build() {
        int n = G.size();
        for (int i = 0; i < n; ++i) if(!used[i]) dfs(i);
        fill(used.begin(),used.end(), 0);
        int k = 0;
        for (int i = n - 1; i >= 0; --i) {
            if(!used[vs[i]]){
                dfs_r(vs[i], k++);
            }
        }
        return k;
    }

    int operator[](int k) const { return cmp[k]; }
};

struct TwoSAT {
    int n;
    SCC scc;
    explicit TwoSAT(int n) : n(n), scc(n*2) {};
    int negate(int v){
        int ret = n+v;
        if(ret >= n*2) ret -= n*2;
        return ret;
    }

    vector<int> build() {
        scc.build();
        vector<int> res(n);
        for (int i = 0; i < n; ++i) {
            if(scc[i] == scc[n+i]) return {};
            res[i] = scc[i] > scc[n+i];
        }
        return res;
    }

    void add_if(int u, int v){ // u -> v
        scc.add_edge(u, v);
        scc.add_edge(negate(v), negate(u));
    }

    void add_or(int u, int v){ // u || v
        add_if(negate(u), v);
    }
};
#line 21 "test/yosupo_twosat.test.cpp"

int main() {
    string s, t;
    int n, m;
    cin >> s >> t >> n >> m;
    TwoSAT G(n);
    for (int i = 0; i < m; ++i) {
        int a, b; char x;
        scanf(" %d %d %c", &a, &b, &x);
        if(a < 0) a = n-a;
        if(b < 0) b = n-b;
        G.add_or(a-1, b-1);
    }
    auto ans = G.build();
    if(ans.empty()){
        puts("s UNSATISFIABLE");
    }else {
        puts("s SATISFIABLE");
        printf("v ");
        for (int i = 0; i < ans.size(); ++i) {
            if(ans[i]) printf("%d ", i+1);
            else printf("%d ", -(i+1));
        }
        puts("0");
    }
    return 0;
}
Back to top page