/
2sat.cpp
71 lines (61 loc) · 1.33 KB
/
2sat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//BEGIN
class TwoSatSolver {
public:
size_t atoms, size;
vector<vector<size_t> > alist, alist_rev;
vector<bool> visited, solution;
list<size_t> post_order;
TwoSatSolver(size_t atoms)
: atoms(atoms), size(atoms * 2 + 1), alist(size),
alist_rev(size), visited(size, false), solution(size, false)
{}
size_t to_index(int a) {
if (a < 0)
a = -a + atoms;
return a;
}
void add_clause(int a, int b) {
a = to_index(a);
b = to_index(b);
add_edge(not_(a), b);
add_edge(not_(b), a);
}
void add_edge(size_t from, size_t to) {
alist[from].push_back(to);
alist_rev[to].push_back(from);
}
void dfs(size_t from) {
visited[from] = true;
for (auto itr : alist[from]) {
if (!visited[itr])
dfs(itr);
}
post_order.push_back(from);
}
void dfsr(size_t from) {
visited[from] = false;
solution[from] = true;
for (auto itr : alist_rev[from]) {
if (visited[itr])
dfsr(itr);
}
}
size_t not_(size_t n) {
if (n > atoms)
return n - atoms;
return n + atoms;
}
bool satisfiable() {
for (size_t i = 1; i < size; i++)
if (!visited[i])
dfs(i);
for (auto it = post_order.rbegin(); it != post_order.rend(); it++)
if (visited[*it] && visited[not_(*it)])
dfsr(*it);
for (size_t i = 1; i <= atoms; i++)
if (solution[i] && solution[not_(i)])
return false;
return true;
}
};
//END