-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2SAT.cpp
More file actions
95 lines (87 loc) · 1.39 KB
/
Copy path2SAT.cpp
File metadata and controls
95 lines (87 loc) · 1.39 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
/// https://infoarena.ro/problema/2sat
#include<bits/stdc++.h>
using namespace std;
ifstream f("2sat.in");
ofstream g("2sat.out");
vector<int>a[200002],inv[200002];
int n,k,kk[200002],ctc[200002];
bool viz[200002];
int val(int x)
{
if(x<0)
return -x+n;
return x;
}
int neg(int x)
{
if(x>n)
return x-n;
return x+n;
}
void add(int x,int y)
{
a[x].push_back(y);
inv[y].push_back(x);
}
void imposibil(int x,int y)
{
add(x,neg(y));
add(y,neg(x));
}
void dfs(int x)
{
viz[x]=1;
for(auto y:a[x])
if(viz[y]==0)
dfs(y);
kk[++k]=x;
}
void dfs_inv(int x)
{
viz[x]=1;
ctc[x]=k;
for(auto y:inv[x])
if(viz[y]==0)
dfs_inv(y);
}
void kosaraju()
{
int i;
for(i=1;i<=2*n;i++)
if(viz[i]==0)
dfs(i);
for(i=1;i<=2*n;i++)
viz[i]=0;
k=0;
for(i=2*n;i>=1;i--)
if(viz[kk[i]]==0)
{
k++;
dfs_inv(kk[i]);
}
}
int main()
{
int m,i,x,y;
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y;
x=val(x);
y=val(y);
imposibil(neg(x),neg(y));
}
kosaraju();
for(i=1;i<=n;i++)
if(ctc[i]==ctc[i+n])
{
g<<-1;
return 0;
}
for(i=1;i<=n;i++)
if(ctc[i]<ctc[i+n])
g<<0<<" ";
else
g<<1<<" ";
return 0;
}