-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2sat.sublime-snippet
87 lines (85 loc) · 1.65 KB
/
2sat.sublime-snippet
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
<snippet>
<content><![CDATA[
class _2sat
{
public:
vector<vector<int>>adj;
int N;
_2sat(int N) // no of nodes
{
adj.resize(N * 2);
this->N = N;
}
void imply(int a, int b) // addedge in base 1 for not write no as -ve
{
if (a < 0)
a = abs(a) + N;
if (b < 0)
b = abs(b) + N;
a--;
b--;
adj[a].push_back(b);
}
void bimply(int a, int b) // same as imply but both side edges
{
if (a < 0)
a = abs(a) + N;
if (b < 0)
b = abs(b) + N;
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
void either(int a, int b)
{
imply(-a, b);
imply(-b, a);
}
bool process()
{
kosaraju kosa(adj);
vector<int>comp(N * 2, -1);
for (int i = 0; i < kosa.clusters.size(); i++)
{
for (auto it : kosa.clusters[i])
{
comp[it] = i;
if (it >= N && comp[it - N] == i)
return false;
if (it < N && comp[it + N] == i)
return false;
}
}
return true;
}
pair<int, vector<bool>> processSoln() // 1 means keep 0 means not keep
{
kosaraju kosa(adj);
vector<int>comp(N * 2, -1);
for (int i = 0; i < kosa.clusters.size(); i++)
{
for (auto it : kosa.clusters[i])
{
comp[it] = i;
if (it >= N && comp[it - N] == i)
return {false, {}};
if (it < N && comp[it + N] == i)
return {false, {}};
}
}
vector<bool>ans(N + 1, false);
for (int i = 0; i < N; i++)
{
if (comp[i] > comp[i + N])
ans[i + 1] = true;
}
return {true, ans};
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>2sat</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>