-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-sat.cpp
130 lines (105 loc) · 2.91 KB
/
2-sat.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "bits/stdc++.h"
using namespace std;
struct two_sat {
int n;
vector<vector<int>> g, gr; // gr is the reversed graph
vector<int> comp, topological_order, answer; // comp[v]: ID of the SCC containing node v
vector<bool> vis;
two_sat() {}
two_sat(int _n) { init(_n); }
void init(int _n) {
n = _n;
g.assign(2 * n, vector<int>());
gr.assign(2 * n, vector<int>());
comp.resize(2 * n);
vis.resize(2 * n);
answer.resize(2 * n);
}
void add_edge(int u, int v) {
g[u].push_back(v);
gr[v].push_back(u);
}
// For the following three functions
// int x, bool val: if 'val' is true, we take the variable to be x. Otherwise we take it to be x's complement.
// At least one of them is true
int neg(int x){
if (x < n)
return x + n;
return x - n;
}
void set(int i, bool f){
if (!f)
add_clause_or(neg(i), neg(i), 1);
else
add_clause_or(i, i, 1);
}
void add_clause_or(int i, int j, bool f) {
if (f){
add_edge(neg(i), j);
add_edge(neg(j), i);
}
else{
set(i, 0);
set(j, 0);
}
}
void add_clause_xor(int i, int j, bool f) {
if (f){
add_clause_or(i, j, 1);
add_clause_or(neg(i), neg(j), 1);
}
else{
add_clause_xor(i, neg(j), 1);
}
}
void add_clause_and(int i, int j, bool f) {
if (f){
set(i, 1);
set(j, 1);
}
else
add_clause_or(neg(i), neg(j), 1);
}
// Topological sort
void dfs(int u) {
vis[u] = true;
for (const auto &v : g[u])
if (!vis[v]) dfs(v);
topological_order.push_back(u);
}
// Extracting strongly connected components
void scc(int u, int id) {
vis[u] = true;
comp[u] = id;
for (const auto &v : gr[u])
if (!vis[v]) scc(v, id);
}
// Returns true if the given proposition is satisfiable and constructs a valid assignment
bool satisfiable() {
fill(vis.begin(), vis.end(), false);
for (int i = 0; i < 2 * n; i++)
if (!vis[i]) dfs(i);
fill(vis.begin(), vis.end(), false);
reverse(topological_order.begin(), topological_order.end());
int id = 0;
for (const auto &v : topological_order)
if (!vis[v]) scc(v, id++);
// Constructing the answer
for (int i = 0; i < n; i++) {
if (comp[i] == comp[i + n]) return false;
answer[i] = (comp[i] > comp[i + n] ? 1 : 0);
}
return true;
}
void print_answer(){
for (int i = 0; i < n; i++)
printf("%d ", answer[i]);
puts("");
}
};
int main(){
int n = 3;
two_sat h(n);
if (h.satisfiable())
h.print_answer();
}