Pagini recente » Cod sursa (job #936390) | Cod sursa (job #1186898) | Cod sursa (job #1253148) | Cod sursa (job #2830754) | Cod sursa (job #2592857)
#include <fstream>
#include <iostream>
using namespace std;
const int MAXN = 100005, MAXM = 200005;
pair<int, int> prop[MAXM];
bool perm[MAXN], ans[MAXN];
int n, m;
bool sat;
void bkt(int k){
if(sat) return;
if(k > n){
sat = 1;
for(int i = 1; i <= m; ++i){
bool x = perm[abs(prop[i].first)], y = perm[abs(prop[i].second)];
if(prop[i].first < 0) x = !x;
if(prop[i].second < 0) y = !y;
sat &= (x | y);
}
if(sat) for(int i = 1; i <= n; ++i) ans[i] = perm[i];
return;
}
for(int i = 0; i <= 1; ++i){
perm[k] = i;
bkt(k + 1);
}
}
int main()
{
ifstream fin("2sat.in");
ofstream fout("2sat.out");
fin >> n >> m;
for(int i = 1; i <= m; ++i){
int x, y;
fin >> x >> y;
prop[i] = {x, y};
}
bkt(1);
if(sat)
for(int i = 1; i <= n; ++i)
fout << ans[i] << " ";
else fout << -1;
return 0;
}