Skip to content

Commit

Permalink
✨ adds equiv and xor to 2 sat
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Jan 25, 2024
1 parent 4ca4ee9 commit f1085ff
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions algorithms/graphs/2-SAT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,30 @@ struct SAT {
return solvable;
}

void add_dis(int a, bool va, int b, bool vb) {
void add_dis(int a, bool va, int b, bool vb) { // a V b
va = !va, vb = !vb;
a = (2 * a) ^ va, b = (2 * b) ^ vb;
int nota = a ^ 1, notb = b ^ 1;
g[nota].emplace_back(b), g[notb].emplace_back(a),
tg[b].emplace_back(nota), tg[a].emplace_back(notb);
}

void add_impl(int a, bool va, int b, int vb) {
void add_impl(int a, bool va, int b, int vb) { // a -> b
add_dis(a, !va, b, vb);
}

void add_equiv(int a, bool va, int b,
bool vb) { // a <-> b
add_impl(a, 1, b, 1);
add_impl(b, 1, a, 1);
add_impl(a, 0, b, 0);
add_impl(b, 0, a, 0);
}

void add_xor(int a, bool va, int b, bool vb) { // a xor b
add_impl(a, 1, b, 0);
add_impl(a, 0, b, 1);
add_impl(b, 1, a, 0);
add_impl(b, 0, a, 1);
}
};

0 comments on commit f1085ff

Please sign in to comment.