Pagini recente » Cod sursa (job #2085370) | Cod sursa (job #1054958) | Cod sursa (job #486345) | Cod sursa (job #2420971) | Cod sursa (job #1213433)
#include <fstream>
#include <bitset>
#include <set>
#include <list>
#include <stack>
#define DIM 100011
using namespace std;
ifstream f("2sat.in");
ofstream g("2sat.out");
int n,m,gr,sol;
int V[2*DIM],low[2*DIM],niv[2*DIM];
set<int> R;
list<int> L[DIM*2];
stack<int> S;
bitset<2*DIM> viz,IS;
void dfs(int nod){
int x;
list<int>::iterator it;
S.push(nod);
low[nod]=niv[nod]=++gr;
viz[nod]=IS[nod]=1;
for(it=L[nod].begin();it!=L[nod].end();it++){
if(!viz[*it]){
dfs(*it);
low[nod]=min(low[nod],low[*it]);
}
else if(IS[*it])
low[nod]=min(low[nod],low[*it]);
}
if(low[nod]==niv[nod]){
R.clear();
do{x=S.top(),S.pop();
IS[x]=0;
if((x<=n && R.find(x+n)!=R.end())||(x>n && R.find(x-n)!=R.end()))
sol=-1;
R.insert(x);
if(V[x]==0)
V[x]=1,(x<=n?V[x+n]=-1:V[x-n]=-1);
}while(x!=nod);
}
}
int main(void){
register int i,j,x,y;
f>>n>>m;
for(i=1;i<=m;i++){
f>>x>>y;
if(x>0){
if(y>0){
L[x+n].push_back(y);
L[y+n].push_back(x);
}
else{
L[x+n].push_back(-y+n);
L[-y].push_back(x);
}
}
else{
if(y>0){
L[-x].push_back(y);
L[y+n].push_back(-x+n);
}
else{
L[-x].push_back(-y+n);
L[-y].push_back(-x+n);
}
}
}
for(i=1;i<=2*n;i++) if(!viz[i]) dfs(i);
if(sol==-1) g<<sol;
else
for(i=1;i<=n;i++)
g<<(V[i]==1?1:0)<<" ";
f.close();
g.close();
return 0;
}