Pagini recente » Cod sursa (job #350397) | Cod sursa (job #1820071) | Cod sursa (job #1919146) | Cod sursa (job #1595115) | Cod sursa (job #2670678)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("2sat.in");
ofstream fout("2sat.out");
vector <int> v[200005];
int n, k;
int llink[200005], idx[200005], ctc[200005], ct;
stack<int> st;
bool inst[200005];
int NEG(int x)
{
return 2 * n - x;
}
void tarjan(int node)
{
llink[node] = idx[node] = ++ k;
inst[node] = true;
st.push(node);
for(int i = 0; i < v[node].size(); i++)
if(idx[v[node][i]] == 0)
{
tarjan(v[node][i]);
llink[node] = min(llink[node], llink[v[node][i]]);
}
else
if(inst[v[node][i]] == true)
llink[node] = min(llink[node], llink[v[node][i]]);
if(idx[node] == llink[node])
{
int naux = st.top();
ctc[naux] = ++ct;
inst[naux] = false;
st.pop();
while(naux != node)
{
naux = st.top();
ctc[naux] = ct;
inst[naux] = false;
st.pop();
}
}
}
int main()
{
int m;
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
v[NEG(x + n)].push_back(y + n);
v[NEG(y + n)].push_back(x + n);
}
for(int i = 0; i <= 2 * n; i++)
if(idx[i] == 0 && i != n)
tarjan(i);
for(int i = n - 1; i >= 0; i--)
if(ctc[i] == ctc[2 * n - i])
{
fout << -1;
return 0;
}
for(int i = n - 1; i >= 0; i--)
if(ctc[i] < ctc[2 * n - i])
fout << 0 << " ";
else
fout << 1 << ' ';
return 0;
}