-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsat.cpp
More file actions
37 lines (37 loc) · 794 Bytes
/
sat.cpp
File metadata and controls
37 lines (37 loc) · 794 Bytes
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
struct Sat {
int n = maxn, col[maxn] = {}, cnt, ver[maxn] = {}, versz, cer[maxn];
vector<int> g[maxn], rg[maxn];
bool mrk[maxn] = {};
void addE(int x, int y) {
g[x].push_back(y);
rg[y].push_back(x);
}
void addOr(int x, int y) {
addE(x ^ 1, y);
addE(y ^ 1, x);
}
void dfsadd(int v){
mrk[v]=1; for(auto &u:g[v])if(!mrk[u])dfsadd(u);
ver[versz++]=v;
}
void dfsset(int v){
col[v]=cnt;
for(auto &u:rg[v])
if(col[u]==-1)
dfsset(u);
}
bool ok() {
memset(mrk, 0, n);
memset(col, -1, n * sizeof col[0]);
for(int v = 0; v < n; v++)
if(!mrk[v])
dfsadd(v);
while(versz)if(col[ver[--versz]]==-1) dfsset(ver[versz]), cnt++;
for(int v = 0; v < n; v += 2)
if(col[v]==col[v^1])
return 0;
else
cer[v] = col[v^1] < col[v];
return 1;
}
} sat;