CPLibrary

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub o06660o/CPLibrary

:heavy_check_mark: src/graph/twosat.hpp

Depends on

Verified with

Code

// 2-SAT 问题: 给定 $n$ 个布尔变量, 与 $m$ 个形式为 $(x_i = f) \lor (x_j = g)$ 的条件.
// 如果有解答案构造在 `ans` 中.
#include "scc.hpp"
struct TwoSat {
  int n;
  vector<char> ans;
  SCC scc;
  TwoSat(int n) : n(n), ans(n), scc(2 * n) {}
  void adde(int i, bool f, int j, bool g) {
    scc.adde(2 * i + !f, 2 * j + g), scc.adde(2 * j + !g, 2 * i + f);
  }
  bool work() {
    scc.work();
    const auto& sccid = scc.sccid;
    for (int i = 0; i < n; i++) {
      if (sccid[2 * i] == sccid[2 * i + 1]) return false;
      ans[i] = sccid[2 * i] > sccid[2 * i + 1];
    }
    return true;
  }
};
#line 1 "src/graph/twosat.hpp"
// 2-SAT 问题: 给定 $n$ 个布尔变量, 与 $m$ 个形式为 $(x_i = f) \lor (x_j = g)$ 的条件.
// 如果有解答案构造在 `ans` 中.
#line 1 "src/graph/scc.hpp"
// 有向图的强连通分量. 返回强联通分量个数, 点对应的强连通分量编号在 `sccid` 中.
struct SCC {
  int n, cur_dfn, cur_scc;
  stack<int> stk;
  vector<int> dfn, low, sccid;
  vector<vector<int>> G;
  SCC(int n) : n(n), dfn(n, -1), low(n), sccid(n, -1), G(n) {}
  void adde(int u, int v) { G[u].PUSHB(v); }
  int work() {
    cur_dfn = cur_scc = 0;
    for (int i = 0; i < n; i++)
      if (dfn[i] == -1) tarjan(i);
    assert(stk.empty());
    return cur_scc;
  }

 private:
  void tarjan(int u) {
    dfn[u] = low[u] = cur_dfn++, stk.push(u);
    for (int v : G[u]) {
      if (dfn[v] == -1) {
        tarjan(v), low[u] = min(low[u], low[v]);
      } else if (sccid[v] == -1) {
        low[u] = min(low[u], dfn[v]);
      }
    }
    if (dfn[u] == low[u]) {
      int v;
      do {
        v = stk.top(), stk.pop(), sccid[v] = cur_scc;
      } while (v != u);
      cur_scc++;
    }
  }
};
#line 4 "src/graph/twosat.hpp"
struct TwoSat {
  int n;
  vector<char> ans;
  SCC scc;
  TwoSat(int n) : n(n), ans(n), scc(2 * n) {}
  void adde(int i, bool f, int j, bool g) {
    scc.adde(2 * i + !f, 2 * j + g), scc.adde(2 * j + !g, 2 * i + f);
  }
  bool work() {
    scc.work();
    const auto& sccid = scc.sccid;
    for (int i = 0; i < n; i++) {
      if (sccid[2 * i] == sccid[2 * i + 1]) return false;
      ans[i] = sccid[2 * i] > sccid[2 * i + 1];
    }
    return true;
  }
};
Back to top page