Cod sursa(job #1114154)

Utilizator Theorytheo .c Theory Data 21 februarie 2014 12:36:11
Problema 2SAT Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>
#include <ctime>
#include <cstdio>
#include <cstdlib>

using namespace std;

#define exp pair<int, int>

ifstream fin ("2sat.in");
ofstream fout ("2sat.out");

const int NMAX = 100009;
const int TRY = 20;

exp V[2 * NMAX]; int C[NMAX];

int N; int M;

int abs(int X){
    if(X < 0) return -X;
    return X;
}

bool compute(int ind) {

    bool a = C[abs(V[ind].first)];
    bool b = C[abs(V[ind].second)];

    if(a < 0) a = !a;
    if(b < 0) b = !b;

    return (a | b);
}

void print() {

    for(int i = 1; i <= N; ++i)
        fout << C[i] << " ";
}

int main() {

    srand(time(0));

    fin >> N >> M;
    for(int i = 1; i <= M; ++i){
        int a, b; fin >> a >> b;
        V[i] = make_pair(a, b);
    }

    for(int i = 1; i <= N; ++i)
        C[i] = rand() % 2;

    for(int j = 1; j <= TRY * N; ++j) {
        int problem = 0;

        for(int i = 1; i <= M; ++i)
            if(!compute(i)) {
                problem = i;
                break;
            }

        if(!problem){ print(); return 0;}

        if(rand() % 2)
            C[abs(V[problem].first)] ^= 1;
        else
            C[abs(V[problem].second)] ^= 1;
    }

    fout << -1;

    return 0;
}