Cod sursa(job #1258325)

Utilizator andreiiiiPopa Andrei andreiiii Data 8 noiembrie 2014 18:50:53
Problema 2SAT Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 2.37 kb
#include <algorithm>
#include <cstdio>

using namespace std;

class P2SAT {
public:
    P2SAT(const int N) {
        Size = 2 * N + 2;
        Offset = N;
        G = vector< vector<int> >(Size);
        Gt = G;
        values = vector<int>(Size, 0);
        used = vector<bool>(Size, false);
    }

    void addRel(int x, int y) {
        addEdge(Offset - x, Offset + y);
        addEdge(Offset - y, Offset + x);
    }

    void addMust(int x) {
        addEdge(Size - 1, Offset + x);
        addEdge(Offset - x, Offset);
    }

    vector<int> getValues() {
        stk.reserve(Size);
        for (int i = 0; i < Size; ++i)
            if (!used[i])
                Dfs1(i);

        fill(used.begin(), used.end(), false);
        Dfs2(Offset);
        for (int i = Size - 1; i >= 0; --i)
            if (!values[stk[i]] && !values[Non(stk[i])])
                Dfs2(stk[i]);

        stk.clear();
        if (values[Offset] == -1) return vector<int>(1, -1);
        for (int i = 0; i <= Offset; ++i)
            if (values[i] == values[Non(i)]) return vector<int>(1, -1);

        vector<int> ret(Offset + 1);
        for (int i = 1; i <= Offset; ++i)
            ret[i] = values[Offset + i];

        return ret;
    }

private:
    vector< vector<int> > G, Gt;
    vector<int> values, stk;
    vector<bool> used;
    int Offset, Size;

    void addEdge(int x, int y) {
        G[x].push_back(y);
        Gt[y].push_back(x);
    }

    void Dfs1(const int node) {
        used[node] = 1;
        for (int p: G[node])
            if (!used[p]) Dfs1(p);

        stk.push_back(node);
    }

    void Dfs2(const int node) {
        used[node] = 1;
        if (values[node])
            values[Offset] = -1;

        values[Non(node)] = 1;
        for (int p: Gt[node])
            if (!used[p]) Dfs2(p);
    }

    int Non(const int n) const {
        if (n == Offset) return Size - 1;
        if (n == Size - 1) return Offset;
        return 2 * Offset - n;
    }
};

int main()
{
    freopen("2sat.in", "r", stdin);
    freopen("2sat.out", "w", stdout);

    int N, M;
    scanf("%d%d", &N, &M);
    P2SAT S(N);

    while (M--) {
        int x, y;
        scanf("%d%d", &x, &y);

        S.addRel(x, y);
    }

    vector<int> sol = S.getValues();
    for (int i = 1; i <= N; ++i)
        printf("%d ", sol[i]);
}