Pagini recente » Cod sursa (job #428920) | Cod sursa (job #1760145) | Cod sursa (job #2001982) | Cod sursa (job #574840) | Cod sursa (job #1542987)
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
ifstream fin("2sat.in");
ofstream fout("2sat.out");
int n, m, G[10001][10001], GT[10001][10001], v[10001], c[10001], L[10001], k;
void citire()
{
int x, y;
fin >> n >> m;
k = 2*n;
for (int i = 1;i <= m;i++)
{
fin >> x >> y;
G[n - x][y+n] = 1;
GT[y + n][n - x] = 1;
G[n - y][x+n] = 1;
GT[x + n][n - y] = 1;
}
}
void dfs(int i)
{
v[i] = 1;
for (int j = 0;j <= 2 * n;j++)
{
if (G[i][j] == 1)
if (v[j] == 0)
dfs(j);
}
L[k] = i;
k--;
}
void dfs2(int i, int t)
{
c[i] = t;
for (int j = 0;j <= 2*n;j++)
if (GT[i][j] == 1)
if (c[j] == 0)
dfs2(j, t);
}
int main()
{
citire();
for (int i = 0;i <= 2 * n;i++) if (i != n)
if(v[i]==0)
dfs(i);
int t = 1;
for (int i = 1;i <= 2*n;i++)
if (c[L[i]]==0)
{
dfs2(L[i], t);
t++;
}
for (int i = 0;i <= 2 * n;i++) if(i!=n)
if (c[i] == c[2 * n - i])
{
fout << -1;
return 0;
}
t--;
int x[10001];
for (int i = 0;i <= 2 * n;i++) if (i != n)
if (c[i] <= t / 2)
x[i] = 0;
else
x[i] = 1;
for (int i = n+1;i <= 2 * n;i++)
fout << x[i]<<' ';
return 0;
}