Pagini recente » Cod sursa (job #2000363) | Cod sursa (job #353380) | Cod sursa (job #3246352) | Cod sursa (job #1498197) | Cod sursa (job #3224489)
#include <bits/stdc++.h>
std::ifstream cin("2sat.in");
std::ofstream cout("2sat.out");
int n,m,x,y,gr[200010],val[200010],low[200010],gri[200010],nrgr[200010];
std::vector<std::vector<int>>a;
std::stack<int>s;
std::vector<std::vector<int>>fullgr;
void ctc(int nod)
{
val[nod]=low[nod]=++low[0];
s.push(nod);
for(auto x:a[nod])
{
if(gr[x]!=0)
continue;
if(low[x]==0)
{
ctc(x);
low[nod]=std::min(low[nod],low[x]);
}
else
low[nod]=std::min(low[nod],val[x]);
}
if(low[nod]==val[nod])
{
++gr[0];
while(s.top()!=nod)
{
fullgr[gr[0]].push_back(s.top());
gr[s.top()]=gr[0];
s.pop();
}
fullgr[gr[0]].push_back(s.top());
gr[s.top()]=gr[0];
s.pop();
}
}
int main()
{
cin>>n>>m;
fullgr.resize(2*n+5);
a.resize(2*n+5);
while(m--)
{
cin>>x>>y;
int xx=0,yy=0;
if(x<0)
{
xx=1;
x*=-1;
}
if(y<0)
{
yy=1;
y*=-1;
}
a[2*x-(1-xx)].push_back(2*y-yy);
a[2*y-(1-yy)].push_back(2*x-xx);
}
for(int i=1;i<=n;++i)
if(gr[i]==0)
ctc(i);
for(int i=1;i<=2*n;++i)
for(auto x:a[i])
if(gr[i]!=gr[x])
++gri[gr[x]];
std::queue<int>q;
for(int i=1;i<=gr[0];++i)
if(gri[i]==0)
q.push(i);
while(!q.empty())
{
int grac=q.front();
q.pop();
nrgr[grac]=++nrgr[0];
for(auto x:fullgr[grac])
for(auto y:a[x])
{
if(gr[y]!=gr[x])
{
--gri[gr[y]];
if(gri[gr[y]]==0)
q.push(gr[y]);
}
}
}
for(int i=1;i<=n;++i)
{
if(nrgr[gr[i*2]]==nrgr[gr[i*2-1]])
{
cout<<-1;
return 0;
}
}
for(int i=1;i<=n;++i)
{
if(nrgr[gr[i*2]]>nrgr[gr[i*2-1]])
cout<<1<<' ';
else
cout<<0<<' ';
}
return 0;
}