-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2 - SAT with solution .cpp
More file actions
192 lines (145 loc) · 4.14 KB
/
Copy path2 - SAT with solution .cpp
File metadata and controls
192 lines (145 loc) · 4.14 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 2- SAT
// a = a and -a = a+n edge; // here n being the number of variable
const int N = 1e5;
vector<int>graph[N];
vector<int>Rgraph[N];
vector<int>forder;
bool vis1[N];
bool vis2[N];
int SCC[N];
vector<bool>assignment(N , false);
void addedge(int a,int b)
{
graph[a].push_back(b);
}
void addRedge(int a,int b)
{
Rgraph[b].push_back(a);
}
void dfs1(int s)
{
vis1[s] = true;
for(auto child:graph[s])
{
if(vis1[child])
{
continue;
}
dfs1(child);
}
forder.push_back(s);
}
void dfs2(int s,int sccno)
{
vis2[s] = true;
SCC[s] = sccno;
for(auto child : Rgraph[s])
{
if(vis2[child])
{
continue;
}
dfs2(child,sccno);
}
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n ;// number of variable
cin>>n;
int m; // number of clause
cin>>m;
int a[m]; // first element of clause
int b[m]; // second element of clause
for(int i=0;i<m;i++)
{
cin>>a[i];
}
for(int i=0;i<m;i++)
{
cin>>b[i];
}
for(int i=0;i<m;i++)
{
if(a[i]>0 && b[i]> 0) // (a v b) = (-a -> b)^(-b -> a)
{
addedge(a[i]+n , b[i]); // (-a -> b)
addRedge(a[i]+n , b[i]);
addedge(b[i]+n , a[i]); //(-b -> a)
addRedge(b[i]+n , a[i]);
}
else if(a[i]>0 && b[i]<0) // (a v -b) = (-a -> -b)^(b -> a)
{
addedge(a[i]+n , n-b[i]); // (-a -> -b)
addRedge(a[i]+n , n-b[i]);
addedge(-b[i], a[i]); // (b -> a)
addRedge(-b[i] , a[i]);
}
else if(a[i]<0 && b[i]>0) // (-a v b) = (a -> b)^(-b -> -a)
{
addedge(-a[i] , b[i]); // (a -> b)
addRedge(-a[i] , b[i]);
addedge(b[i]+n, n-a[i]); // (-b -> -a)
addRedge(b[i]+n , n-a[i]);
}
else //(-a v -b) = (a -> -b)^(b -> -a)
{
addedge(-a[i] , n-b[i]); // (a -> -b)
addRedge(-a[i] , n-b[i]);
addedge(-b[i] , n-a[i]); // (b -> -a)
addRedge(-b[i] , n-a[i]);
}
}
for(int i=1;i<=2*n;i++)
{
if(vis1[i]==false)
{
dfs1(i);
}
}
reverse(forder.begin(),forder.end());
int cnt=0;
for(auto x : forder)
{
if(vis2[x]==false)
{
cnt++;
dfs2(x,cnt);
}
}
bool ans = true;
for(int i=1;i<=n;i++)
{
if(SCC[i]==SCC[i+n])
{
ans = false;
}
assignment[i] = SCC[i+n]<SCC[i]; // ( -x -> x) hole x = true else (x -> -x) hole x = false [for testing build a truth table]
}
if(ans==false)
{
cout<<"The given expression is NOT satisfiable."<<endl;
return 0;
}
cout<<("The given expression is satisfiable.")<<endl;
cout<<"The solution: "<<endl;
for(int i=1;i<=n;i++)
{
cout<<assignment[i]<<" ";
}
cout<<endl;
return 0;
}
/*
5 7
1 -2 -1 3 -3 -4 -3
2 3 -2 4 5 -5 4
output : The given expression is satisfiable
The solution:
1 0 0 1 0
2 4
1 -1 1 -1
2 2 -2 -2
output : The given expression is NOT satisfiable
*/