Pagini recente » Cod sursa (job #708522) | Cod sursa (job #1391966) | Rating Daria Ciolacu (Daria_Ciolacu) | Cod sursa (job #2832878) | Cod sursa (job #2499511)
#include <fstream>
#include <vector>
#include <stack>
#include <set>
#define DIM 200010
using namespace std;
ifstream fin ("2sat.in");
ofstream fout ("2sat.out");
vector <int> L[DIM];
stack <int> s;
set <int> v;
int niv[DIM],low[DIM],viz[DIM],w[DIM];
int n,m,x,y,nx,ny,i,idx,sol;
void dfs (int nod){
viz[nod] = 1;
low[nod] = niv[nod] = ++idx;
s.push(nod);
for (int i=0;i<L[nod].size();i++){
int vecin = L[nod][i];
if (!niv[vecin]){
dfs (vecin);
low[nod] = min (low[nod],low[vecin]);
} else {
if (viz[vecin])
low[nod] = min (low[nod],low[vecin]);
}}
if (low[nod] == niv[nod]){
v.clear();
int x;
do{
x = s.top();
int nx = (x <= n) ? (x) : (x-n);
if (v.find(nx) != v.end())
sol = -1;
v.insert (x);
if (w[x] == 0){
w[x] = 1;
if (x <= n)
w[x+n] = -1;
else w[x-n] = -1;
}
viz[x] = 0;
s.pop();
} while (x != nod);
}
}
int main (){
fin>>n>>m;
for (i=1;i<=m;i++){
fin>>x>>y;
nx = (x > 0) ? (x+n) : (-x);
ny = (y > 0) ? (y+n) : (-y);
if (x < 0)
x = -x+n;
if (y < 0)
y = -y+n;
L[nx].push_back(y);
L[ny].push_back(x);
}
for (i=1;i<=2*n;i++)
if (!viz[i])
dfs (i);
if (sol == -1){
fout<<-1;
return 0;
}
for (i=1;i<=n;i++){
if (w[i] == -1)
fout<<0<<" ";
else fout<<1<<" ";
}
return 0;
}