-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy path2-SAT 2.cpp
105 lines (92 loc) · 2.23 KB
/
2-SAT 2.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
// for x or y add !x -> y, !y -> x
// x and y = (!x or y) and (x or !y) and (!x or !
/*
inline void add_implication(int a, int b){
if (a < 0) a = n - a;
if (b < 0) b = n - b;
adj[a].push_back(b);
rev[b].push_back(a);
}
inline void add_or(int a, int b){
add_implication(-a, b);
add_implication(-b, a);
}
inline void add_xor(int a, int b){
add_or(a, b);
add_or(-a, -b);
}
inline void add_and(int a, int b){
add_or(a, b);
add_or(a, -b);
add_or(-a, b);
}
/// force variable x to be true (if x is negative, force !x to be true)
inline void force_true(int x){
if (x < 0) x = n - x;
add_implication(neg(x), x);
}
/// force variable x to be false (if x is negative, force !x to be false)
inline void force_false(int x){
if (x < 0) x = n - x;
add_implication(x, neg(x));
}
*/
struct tSAT{
int n, id[MAX][2];
vi G[MAX];
int ord, dis[MAX], low[MAX], sid[MAX], scc;
stack <int> s;
tSAT(int n): n(n){
int now=0;
f(i,1,n+1){
f(j,0,2){
id[i][j]=++now;
}
}
}
tSAT() {}
void add_edge(int u,int tu,int v,int tv){
G[id[u][tu]].pb(id[v][tv]);
}
bool feasible(){
scc=0; ord=0;
mem(dis,0); mem(sid,0);
f(i,1,2*n+1){
if(!dis[i]) tarjan(i);
}
f(i,1,n+1){
if(sid[id[i][0]]==sid[id[i][1]]) return false;
}
return true;
}
vi solution(){
vi ans;
f(i,1,n+1){
if(sid[id[i][0]]>sid[id[i][1]]) ans.pb(i);
}
return ans;
}
void tarjan(int u){
s.push(u);
dis[u]=low[u]=++ord;
f(i,0,G[u].size()){
int v=G[u][i];
if (!dis[v]){
tarjan(v);
low[u]=min(low[v],low[u]);
}
else if (!sid[v]){
low[u]=min(dis[v],low[u]);
}
}
if (low[u]==dis[u]){
++scc;
while(1){
int t=s.top();
s.pop();
sid[t]=scc;
if (t==u) break;
}
}
}
}