Pagini recente » Cod sursa (job #2000340) | Cod sursa (job #1656864) | Cod sursa (job #1274852) | Cod sursa (job #2129069) | Cod sursa (job #2942344)
///x sau y
///~x->y && ~y->x
///colorare ctc
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
ifstream cin ("2sat.in");
ofstream cout ("2sat.out");
const int N = 2e5;
int ctc[N + 1], sol[N + 1];
bool viz[N + 1];
queue <int> q;
vector <int> g[N + 1], gb[N + 1], v;
int n, m, x, y, k;
int negare (int x)
{
return (x > n) ? (x - n) : (x + n);
}
int valnod (int x)
{
return (x < 0) ? n - x : x;
}
void dfsplus (int node)
{
viz[node] = 1;
for (auto it : g[node])
if (!viz[it])
dfsplus(it);
v.push_back(node);
}
void dfsminus (int node)
{
ctc[node] = k;
for (auto it : gb[node])
if (!ctc[it])
dfsminus(it);
}
int main()
{
for (cin >> n >> m; m && cin >> x >> y; --m)
{
x = valnod(x);
y = valnod(y);
g[negare(x)].push_back(y);
g[negare(y)].push_back(x);
gb[y].push_back(negare(x));
gb[x].push_back(negare(y));
}
for (int i = 1; i <= (n << 1); ++i)
if (!viz[i])
dfsplus(i);
reverse (v.begin(), v.end());
for (auto it : v)
if (!ctc[it])
++k, dfsminus (it);
for (int i = 1; i <= n; ++i)
if (ctc[i] == ctc[i + n])
{
cout << "-1";
return 0;
}
for (int i = 1; i <= n; ++i)
cout << (ctc[i] < ctc[i + n]) << ' ';
return 0;
}