Pagini recente » Cod sursa (job #3005702) | Cod sursa (job #1834677) | Cod sursa (job #2380181) | Cod sursa (job #1280318) | Cod sursa (job #2218398)
#include <bits/stdc++.h>
using namespace std;
ifstream in("2sat.in");
ofstream out("2sat.out");
map<int, vector<int> > v,vt;
const int NMAX = 100005;
bool contr;
map<int,int> viz;
int nr;
stack<int> st;
void dfp(int x)
{
viz[x] = 1;
for (auto it: v[x])
if (!viz[it])
dfp(it);
st.push(x);
}
void dfm(int x)
{
viz[x] = nr;
for (auto it: vt[x])
if (!viz[it])
dfm(it);
}
int main()
{
int n,m;
in >> n >> m;
for (int i = 1; i<=m; i++)
{
int x,y;
in >> x >> y;
v[-x].push_back(y);
v[-y].push_back(x);
vt[y].push_back(-x);
vt[x].push_back(-y);
}
for (int i = -n; i<=n; i++)
if (!viz[i])
dfp(i);
viz.clear();
while (!st.empty())
{
int now = st.top();
if (!viz[now])
{
nr++;
dfm(now);
}
st.pop();
}
for (int i = 1; i<=n; i++)
if (viz[i] == viz[-i])
contr = 1;
if (contr)
out << "-1";
else
{
for (int i = 1; i<=n; i++)
if (viz[i]<viz[-i])
out << "0 ";
else
out << "1 ";
}
}