Cod sursa(job #2592857)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 2 aprilie 2020 15:52:14
Problema 2SAT Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#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;
}