Pagini recente » Cod sursa (job #2176090) | Cod sursa (job #3125063) | Cod sursa (job #1756716) | Cod sursa (job #2735770)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("2sat.in");
ofstream fout ("2sat.out");
struct nodS
{
int low, id;
bool on, viz;
vector<int> v;
};
int n,m,t,x,y;
nodS nod[200005];
int ans[200005];
vector<int> s;
vector<vector<int>> v;
void add_edge(int x, int y)
{
if(x<0)
x=n-x;
if(y<0)
y=n-y;
nod[x].v.push_back(y);
}
int inv(int x)
{
if(x<=n)
return x+n;
else
return x-n;
}
void dfs(int p, int par)
{
nod[p].viz=true;
t++;
nod[p].id=nod[p].low=t;
nod[p].on=true;
s.push_back(p);
for(int it : nod[p].v)
{
if(it==par)
continue;
if(!nod[it].viz)
{
dfs(it, p);
nod[p].low=min(nod[p].low, nod[it].low);
}
else if(nod[it].on)
nod[p].low=min(nod[p].low, nod[it].id);
}
if(nod[p].low==nod[p].id)
{
vector<int> nv;
while(true)
{
int x=s.back();
s.pop_back();
nv.push_back(x);
nod[x].on=false;
if(x==p)
break;
}
v.push_back(nv);
}
}
void scc()
{
for(int i=1;i<=2*n;i++)
if(!nod[i].viz)
dfs(i, 0);
reverse(v.begin(), v.end());
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>x>>y;
add_edge(-x, y);
add_edge(-y, x);
}
scc();
for(int i=1;i<=2*n;i++)
ans[i]=-1;
for(auto &gr : v)
{
bool val=false;
for(int it : gr)
if(ans[inv(it)]==0)
val=true;
for(int it : gr)
ans[it]=val;
}
for(int i=1;i<=n;i++)
fout<<ans[i]<<' ';
return 0;
}