Pagini recente » Cod sursa (job #1943472) | Cod sursa (job #2608055) | Cod sursa (job #967219) | Cod sursa (job #1208218) | Cod sursa (job #1654899)
#include <fstream>
using namespace std;
ifstream in("2sat.in");
ofstream out("2sat.out");
#define N 100001
#define M 200001
int n, m, dublun;
int lst[2 * N], vf[4 * M], urm[4 * M], nvf = 0;
bool intoarcere[4 * M];
bool ok1[2 * N];
int st[2 * N], nst = 0;
bool ok2[2 * N];
bool sol[2 * N];
inline void dfs1(int x)
{
ok1[x] = 1;
for(int i = lst[x]; i; i = urm[i])
if(!intoarcere[i] && !ok1[vf[i]])
dfs1(vf[i]);
st[++nst] = x;
}
inline void dfs2(int x)
{
ok2[x] = 1;
sol[dublun - x] = 1;
for(int i = lst[x]; i; i = urm[i])
if(intoarcere[i] && !ok2[vf[i]])
dfs2(vf[i]);
}
int main()
{
in >> n >> m;
dublun = 2 * n + 1;
while(m--)
{
int x, y;
in >> x >> y;
if(x < 0)
x += dublun;
if(y < 0)
y += dublun;
int notx = dublun - x, noty = dublun - y;
vf[++nvf] = y;
urm[nvf] = lst[notx];
lst[notx] = nvf;
vf[++nvf] = notx;
urm[nvf] = lst[y];
intoarcere[nvf] = 1;
lst[y] = nvf;
vf[++nvf] = x;
urm[nvf] = lst[noty];
lst[noty] = nvf;
vf[++nvf] = noty;
urm[nvf] = lst[x];
intoarcere[nvf] = 1;
lst[x] = nvf;
}
for(int i = 1; i < dublun; i++)
if(!ok1[i])
dfs1(i);
for(int i = nst; i >= 1; i--)
if(!ok2[st[i]] && !ok2[dublun - st[i]])
dfs2(st[i]);
for(int i = 1; i <= n; i++)
if(sol[i] == sol[dublun - i])
{
out << "-1\n";
return 0;
}
for(int i = 1; i <= n; i++)
out << sol[i] << ' ';
out << '\n';
return 0;
}