-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2SAT.cpp
More file actions
99 lines (89 loc) · 1.59 KB
/
2SAT.cpp
File metadata and controls
99 lines (89 loc) · 1.59 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
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int kN = 2e5;
vector<int> g[1 + kN];
int n, m, timer, vf, N, tin[1 + kN], low[1 + kN], stk[1 + kN], ctcIndex[1 + kN];
void minSelf(int &x, int y) {
if (y < x) {
x = y;
}
}
int neg(int i) {
if (i <= n) {
return i + n;
}
return i - n;
}
void addEdge(int i, int j) {
g[i].emplace_back(j);
}
void addOr(int i, int j) {
addEdge(neg(i), j);
addEdge(neg(j), i);
}
int getNode(char c, int x) {
if (c == '+') {
return x;
}
return x + n;
}
void dfs(int u) {
tin[u] = ++timer;
low[u] = timer;
stk[++vf] = u;
for (int v : g[u]) {
if (tin[v]) {
minSelf(low[u], tin[v]);
} else {
dfs(v);
minSelf(low[u], low[v]);
}
}
if (tin[u] == low[u]) {
int node;
do {
node = stk[vf];
tin[node] = INF;
ctcIndex[node] = N;
vf -= 1;
} while (node != u);
N += 1;
}
}
void testCase() {
cin >> m >> n;
for (int i = 0; i < m; ++i) {
char c1, c2;
int i1, i2;
cin >> c1 >> i1 >> c2 >> i2;
addOr(getNode(c1, i1), getNode(c2, i2));
}
for (int i = 1; i <= 2 * n; ++i) {
if (!tin[i]) {
dfs(i);
}
}
for (int i = 1; i <= n; ++i) {
if (ctcIndex[i] == ctcIndex[i + n]) {
cout << "IMPOSSIBLE\n";
return;
}
}
for (int i = 1; i <= n; ++i) {
if (ctcIndex[i] < ctcIndex[i + n]) {
cout << '+';
} else {
cout << '-';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}