-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2sat.cpp
More file actions
64 lines (51 loc) · 1.35 KB
/
2sat.cpp
File metadata and controls
64 lines (51 loc) · 1.35 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
// https://www.codechef.com/viewsolution/28213034
// https://www.codechef.com/viewsolution/28213915
const int MAXN = 2e5 + 10;
int n,cnt;
vector<int> graph[MAXN],Rgraph[MAXN];
bool visit[MAXN];
vector<int> order;
int comp[MAXN];
struct TwoSat {
void init(){
order.clear();
for(int i=0;i<=2*n;i++){
graph[i].clear(); Rgraph[i].clear();
}
}
void add(int u,int v){
graph[u].push_back(v);
Rgraph[v].push_back(u);
}
void dfs1(int u){
visit[u] = true;
for(int v:graph[u]){
if(!visit[v]) dfs1(v);
}
order.push_back(u);
}
void dfs2(int u,int c){
comp[u] = c;
for(int v:Rgraph[u]){
if(comp[v] == -1) dfs2(v,c);
}
}
int solve(vector<int> &ans){
memset(visit,0,sizeof(visit));
for(int i=1;i<=2*n;i++) if(!visit[i]) dfs1(i);
memset(comp,-1,sizeof(comp));
reverse(order.begin(),order.end());
int grp = 1;
for(int x:order){
if(comp[x] == -1) dfs2(x,grp++);
}
// COMPARING NEGATIONS COMPONENTS
for(int i=1;i<=n;i++) if(comp[i] == comp[i+n]) return 0;
ans.clear();
for(int i=1;i<=n;i++){
int choose = (comp[i] > comp[i+n])?i:(n+i);
ans.push_back(choose);
}
return 1;
}
};