Pagini recente » Istoria paginii utilizator/nightban3 | Cod sursa (job #2051714) | Profil AlessiaFrunza | Cod sursa (job #726719) | Cod sursa (job #1229042)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
#define cout g
#define nmax 100010
ifstream f("2sat.in");
ofstream g("2sat.out");
int n,m,i,a,b,r,st;
vector <int> G[nmax*2],G_t[nmax*2],ctc[nmax];
char x[nmax];
bool viz[2*nmax];
int stack[nmax];
#define G (G+nmax)
#define G_t (G_t+nmax)
#define viz (viz+nmax)
#define foor(i,a,b) for(__typeof(a) i=a;i!=b;++i)
void dfs(int nod)
{
viz[nod]=true;
foor(it,G[nod].begin(),G[nod].end())
if (!viz[*it]) dfs(*it);
stack[++st]=nod;
}
void dfs_t(int nod)
{
viz[nod]=false;
ctc[r].push_back(nod);
foor(it,G_t[nod].begin(),G_t[nod].end())
if (viz[*it]) dfs_t(*it);
}
int main()
{
f>>n>>m;
memset(x,2,n+1);
for(i=1; i<=m; ++i)
{
f>>a>>b;
G[-a].push_back(b);
G[-b].push_back(a);
G_t[b].push_back(-a);
G_t[a].push_back(-b);
}
for(i=-n; i<=n; ++i)
if(!viz[i]) dfs(i);
for(; st; --st)
if(viz[stack[st]])
{
++r;
dfs_t(stack[st]);
}
bool s=false;
for(i=1; i<=r; ++i)
{
foor(it,ctc[i].begin(),ctc[i].end())
{
if(*it<0)
{
if(x[-*it]<2) if(x[-*it]!=(!s))
{
cout<<-1;
return 0;
}
else;
else x[-*it]=!s;
}
else
{
if(x[*it]<2) if(x[*it]!=s)
{
cout<<-1;
return 0;
}
else;
else x[*it]=s;
}
}
--r;
}
for(i=1;i<=n;++i) cout<<(int)x[i]<<' ';
return 0;
}