Skip to content

Commit d5bde71

Browse files
committed
fix: updated two-sat interfaces
1 parent 0667f64 commit d5bde71

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/alfred/all

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "graph/graph.hpp"
2626
#include "graph/grid.hpp"
27+
#include "graph/twosat.hpp"
2728
#include "graph/hld.hpp"
2829
#include "graph/lca.hpp" // Will be included after graph class is implemented well enough
2930
#include "graph/maxflow.hpp"

src/alfred/graph/twosat.hpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
struct TwoSAT {
77
std::vector<std::vector<int>> G;
88
std::vector<int> dfn, low, stk, bel, in;
9-
TwoSAT(void) = default;
9+
// TwoSAT(void) = default;
1010
TwoSAT(int n) : G(2 * n) {}
1111
void tarjan(int u, int &cnt, int &cc) {
1212
dfn[u] = low[u] = ++cnt;
@@ -30,11 +30,32 @@ struct TwoSAT {
3030
}
3131
// Add constraint: x = a or y = b.
3232
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);
33+
// size_t should = 2 * (std::max(x, y) + 1);
34+
// if (G.size() < should) G.resize(should);
3535
G[x << 1 | !a].push_back(y << 1 | b);
3636
G[y << 1 | !b].push_back(x << 1 | a);
3737
}
38+
// Add constraint: x = a => y = b;
39+
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);
42+
G[x << 1 | a].push_back(y << 1 | b);
43+
}
44+
inline void same(int x, int y) {
45+
conduct(x, true, y, true);
46+
conduct(y, true, x, true);
47+
conduct(x, false, y, false);
48+
conduct(y, false, x, false);
49+
}
50+
inline void diff(int x, int y) {
51+
conduct(x, true, y, false);
52+
conduct(y, true, x, false);
53+
conduct(x, false, y, true);
54+
conduct(y, false, x, true);
55+
}
56+
inline void set(int x, bool v) {
57+
conduct(x, !v, x, v);
58+
}
3859
inline void init(void) { // find scc.
3960
int n = G.size(), cnt = 0, cc = 0;
4061
in = dfn = low = bel = std::vector<int>(n, 0);

0 commit comments

Comments
 (0)