44#include < vector>
55
66struct TwoSAT {
7+ const int n;
78 std::vector<std::vector<int >> G;
89 std::vector<int > dfn, low, stk, bel, in;
9- // TwoSAT(void) = default;
10- TwoSAT (int n) : G(2 * n) {}
10+ TwoSAT (int _n) : n(_n), G(2 * _n) {} // 0-indexed, n nodes.
1111 void tarjan (int u, int &cnt, int &cc) {
1212 dfn[u] = low[u] = ++cnt;
1313 in[u] = 1 , stk.push_back (u);
@@ -28,34 +28,30 @@ struct TwoSAT {
2828 } while (v != u);
2929 }
3030 }
31- // Add constraint: x = a or y = b.
32- inline void add (int x, bool a, int y, bool b) {
33- // size_t should = 2 * (std::max(x, y) + 1);
34- // if (G.size() < should) G.resize(should);
35- G[x << 1 | !a].push_back (y << 1 | b);
36- G[y << 1 | !b].push_back (x << 1 | a);
37- }
38- // Add constraint: x = a => y = b;
31+ // Add constraint: res[x] = a => res[y] = b.
3932 inline void conduct (int x, bool a, int y, bool b) {
40- // size_t should = 2 * (std::max(x, y) + 1);
41- // if (G.size() < should) G.resize(should);
4233 G[x << 1 | a].push_back (y << 1 | b);
4334 }
35+ // Add constraint: res[x] = a or res[y] = b.
36+ inline void add (int x, bool a, int y, bool b) {
37+ conduct (x, !a, y, b), conduct (y, !b, x, a);
38+ }
39+ // Add constraint: res[x] = res[y].
4440 inline void same (int x, int y) {
4541 conduct (x, true , y, true );
4642 conduct (y, true , x, true );
4743 conduct (x, false , y, false );
4844 conduct (y, false , x, false );
4945 }
46+ // Add constraint: res[x] != res[y].
5047 inline void diff (int x, int y) {
5148 conduct (x, true , y, false );
5249 conduct (y, true , x, false );
5350 conduct (x, false , y, true );
5451 conduct (y, false , x, true );
5552 }
56- inline void set (int x, bool v) {
57- conduct (x, !v, x, v);
58- }
53+ // Add constraint: res[x] = v.
54+ inline void set (int x, bool v) { conduct (x, !v, x, v); }
5955 inline void init (void ) { // find scc.
6056 int n = G.size (), cnt = 0 , cc = 0 ;
6157 in = dfn = low = bel = std::vector<int >(n, 0 );
@@ -72,7 +68,7 @@ struct TwoSAT {
7268 }
7369 return true ;
7470 }
75- std::vector<int > solve (int n ) {
71+ std::vector<int > solve (void ) {
7672 int var = G.size () / 2 ;
7773 std::vector<int > sol (n);
7874 for (int i = 0 ; i < var; i++) {
0 commit comments