forked from spaghetti-source/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsatisfiability.cc
More file actions
160 lines (151 loc) · 3.84 KB
/
satisfiability.cc
File metadata and controls
160 lines (151 loc) · 3.84 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// SAT solver (DPLL method)
//
// Description
// We are given a CNF, e.g.,
// \phi(x) = (x_1 or ~x_2) and (x_3 or ~x_4 or ~x_5) and ... .
// SAT finds an assignment x for phi(x) = true.
//
// Algorithm:
// Davis-Putnum-Logemann-Loveland's algorithm that performs
// 1) unit propagation,
// 2) pure literal elimination,
// 3) branch and search.
//
// Complexity:
// O(2^n) in worst case.
// This implementation is practical for n = 100.
//
// Verified:
// Uniform 3-SAT instances from http://www.cs.ubc.ca/~hoos/SATLIB/benchm.html
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <functional>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
#define dout if(0){}else cout
// positive literal x in [0,n),
// negative literal ~x in [-n,0)
//
struct satisfiability {
int n;
vector<int> occ, pos, neg;
vector<vector<int>> adj, lit;
satisfiability(int n) : n(n), adj(2*n), occ(2*n) { }
void add_clause(const vector<int> &c) {
for (auto u: c) {
adj[u+n].push_back(lit.size());
occ[u+n] += 1;
}
lit.push_back(c);
}
vector<bool> x;
vector<vector<int>> decision_stack;
vector<int> unit_stack, pure_stack;
void push(int u) {
x[u+n] = 1;
decision_stack.back().push_back(u);
for (auto i: adj[ u+n])
if (pos[i]++ == 0)
for (auto u: lit[i])
--occ[u+n];
for (auto i: adj[~u+n]) {
++neg[i];
if (pos[i] == 0) unit_stack.push_back(i);
}
}
void pop() {
int u = decision_stack.back().back();
decision_stack.back().pop_back();
x[u+n] = 0;
for (auto i: adj[ u+n])
if (--pos[i] == 0)
for (auto u: lit[i])
++occ[u+n];
for (auto i: adj[~u+n]) --neg[i];
}
bool reduction() {
while (!unit_stack.empty() || !pure_stack.empty()) {
if (!pure_stack.empty()) { // pure literal elimination
int u = pure_stack.back();
pure_stack.pop_back();
if (occ[u+n] == 1 && occ[~u+n] == 0) push(u);
} else { // unit propagation
int i = unit_stack.back();
unit_stack.pop_back();
if (pos[i] > 0) continue;
if (neg[i] == lit[i].size()) return false;
if (neg[i] + 1 == lit[i].size()) {
int w = n;
for (int u: lit[i]) if (!x[u+n] && !x[~u+n]) w = u;
if (x[~w+n]) return false;
push(w);
}
}
}
return true;
}
bool solve() {
x.assign(2*n,0);
pos = neg = vector<int>(lit.size());
decision_stack.assign(1,{});
while (1) {
if (reduction()) {
int s = 0;
for (int u = 0; u < n; ++u)
if (occ[s+n]+occ[~s+n] < occ[u+n]+occ[~u+n]) s = u;
if (occ[s+n] + occ[~s+n] == 0) return true;
decision_stack.push_back({});
push(s);
} else {
int s = decision_stack.back()[0];
while (!decision_stack.back().empty()) pop();
decision_stack.pop_back();
if (decision_stack.empty()) return false;
push(~s);
}
}
}
};
void moin(int seed) {
#if 1
vector<vector<int>> input;
int n;
for (char s[1024]; fgets(s, sizeof(s), stdin); ) {
if (s[0] == 'c') continue;
if (s[0] == 'p') {
sscanf(s, "p cnf %d %*d", &n);
} else if (s[0] == '%') {
break;
} else {
int u, v, w;
sscanf(s, "%d %d %d", &u, &v, &w);
input.push_back({u, v, w});
}
}
satisfiability solver(n);
for (auto &c: input) {
for (auto &a: c)
if (a >= 0) a -= 1;
solver.add_clause(c);
}
int x = solver.solve();
cout << x << endl;
#else
srand(seed);
satisfiability solver(9);
solver.add_clause({0,1,5});
solver.add_clause({0,1,6});
solver.add_clause({0,1,7});
solver.add_clause({0,1,8});
cout << solver.solve() << endl;
#endif
}
int main() {
moin(0);
}