Cod sursa(job #1113930)

Utilizator Theorytheo .c Theory Data 21 februarie 2014 02:34:37
Problema 2SAT Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.06 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

#define exp pair<short int, short int>
#define attribute pair<int, bool>

//O(N * M)

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

const int NMAX = 100009;

int N; int M;

exp V[NMAX * 2];

int Next_Values[NMAX]; int Values[NMAX];

vector<int> G[NMAX], C[NMAX];

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


void read() {

    fin >> N >> M;
    for(int i = 1; i <= M; ++i) {
        int a; int b;
        fin >> a >> b;

        V[i] = make_pair(a, b);
        G[abs(a)].push_back(i);
        G[abs(b)].push_back(i);
        C[abs(a)].push_back(b);
        C[abs(b)].push_back(a);
    }
}

bool compute(int a, int b, int ind) {

    --a ; --b;
    if(V[ind].first < 0) a = (a + 1) % 2;
    if(V[ind].second < 0) b = (b + 1) % 2;

    return (a || b);
}

bool solve(int pos, int val) {

    queue<int> Q; bool viz[NMAX];

    memset(viz, 0, sizeof(viz));

    Q.push(pos);
    viz[pos] = true;
    Next_Values[pos] = val;

    while(!Q.empty()) {

        int a = Q.front();
        Q.pop();

        for(unsigned i = 0 ;i < C[a].size(); ++i) {
            int b = abs(C[a][i]);
            int ind = G[a][i];

            if(viz[b] == false){

                int ant = Next_Values[b];
                bool pun = 0;

                Next_Values[b] = 2;
                if(!compute(Next_Values[a], Next_Values[b], ind))
                    pun = 1;
                Next_Values[b] = 1;
                if(!compute(Next_Values[a], Next_Values[b], ind))
                    pun = 1;
                if(pun) {

                    Next_Values[b] = 1;
                    if(!compute(Next_Values[a], Next_Values[b], ind))
                        Next_Values[b] = 2;
                    viz[b] = true;
                    Q.push(b);

                } else
                    Next_Values[b] = ant;

            } else {

                if(!compute(Next_Values[a], Next_Values[b], ind))
                    return 0;
            }
        }
    }

    return 1;

}

int main() {

    read();

    for(int i = 1; i <= N; ++i) {
        if(Values[i] == 0) {
            memcpy(Next_Values, Values, sizeof(Values));
            if(solve(i, 1)) {
                memcpy(Values, Next_Values, sizeof(Next_Values));
            } else if(solve(i, 2)) memcpy(Values, Next_Values, sizeof(Next_Values));
                    else {fout << -1; return 0;}
        } else {

            memcpy(Next_Values, Values, sizeof(Values));
            if(solve(i, Next_Values[i]))
                memcpy(Values, Next_Values, sizeof(Values));
            else {
                memcpy(Next_Values, Values, sizeof(Values));
                if(solve(i, (Next_Values[i] + 1) % 3 + 1))
                    memcpy(Values, Next_Values, sizeof(Values));
                else {fout << -1; return 0;}
            }
        }
    }

    for(int i = 1; i <= N; i++)
        fout << Values[i] - 1<<" ";
    return 0;

}