Cod sursa(job #2020729)

Utilizator MaligMamaliga cu smantana Malig Data 11 septembrie 2017 16:44:13
Problema 2SAT Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.54 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#include <deque>
#include <ctime>
#include <queue>
#include <cmath>

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
using zint = int;
using namespace std;
const int inf = 3e4 + 5;
const int NMax = 1e5 + 5;
const int MMax = 2e5 + 5;
ifstream in("2sat.in");
ofstream out("2sat.out");

zint N,M,nrComp;
zint compOf[2*NMax],ans[NMax];
bool visStack[2*NMax],inComp[2*NMax],visComp[2*NMax];
pair<zint,zint> ex[MMax];
vector<zint> vEdge[2*NMax],vRev[2*NMax],vComp[2*NMax],comp[2*NMax];
stack<zint> st;

void getStack(zint);
void getComp(zint);
void getEdges(zint);
void update(zint,const zint);

void printComp(zint c) {
    //*
    //for (zint c=1;c <= nrComp;++c) {
        for (zint node : comp[c]) {
            cout<<node<<' ';
        }
        cout<<'\n';
    //}
    cout<<"\n";
    //*/

    /*
    for (zint c=1;c <= nrComp;++c) {
        for (zint nxt : vComp[c]) {
            cout<<nxt<<' ';
        }
        cout<<'\n';
    }
    //*/
}

bool eval(zint nr) {
    zint x = ex[nr].first,
        y = ex[nr].second,
        val1 = ans[abs(x)],
        val2 = ans[abs(y)];
    if (x < 0) { val1 ^= 1; }
    if (y < 0) { val2 ^= 1; }

    return (val1 | val2);
}

int main() {
    in>>N>>M;
    for (int i=1;i <= M;++i) {
        zint x,y;
        in>>x>>y;

        ex[i] = mp(x,y);
        //cout<<x<<' '<<y<<'\n';
        x = (x < 0) ? -x + N : x;
        y = (y < 0) ? -y + N : y;
        //cout<<x<<' '<<y<<"\n\n";

        zint invx = (x > N) ? x-N : x+N,
             invy = (y > N) ? y-N : y+N;
        //cout<<invx<<' '<<invy<<"\n\n";

        vEdge[invx].pb(y);
        vRev[y].pb(invx);
        vEdge[invy].pb(x);
        vRev[x].pb(invy);

        /*
        cout<<invx<<' '<<y<<'\n';
        cout<<invy<<' '<<x<<'\n';
        cout<<"\n\n";
        //*/

        /*
        out<<-x<<' '<<y<<'\n';
        out<<-y<<' '<<x<<'\n';
        //*/
    }

    /*
    for (int i=1;i <= N;++i) {
        for (int nxt : vEdge[i]) {
            cout<<nxt<<' ';
        }
        cout<<'\n';
    }
    cout<<"\n\n";
    //*/

    //inComp[0] = true;
    for (zint i = 1;i <= 2*N;++i) {
        if (visStack[i]) {
            continue;
        }

        getStack(i);
    }

    while (st.size()) {
        zint node = st.top(); st.pop();
        if (!inComp[node]) {
            ++nrComp;
            getComp(node);
        }
    }

    bool sol = true;
    for (zint i=1;i <= N;++i) {
        if (compOf[i] == compOf[i+N]) {
            sol = false;
            break;
        }
    }

    if (sol) {
        for (zint c=1;c <= nrComp;++c) {
            //printComp(c);
            if (visComp[c]) {
                continue;
            }

            update(c,0);
            //update(comp2,1);
        }

        for (zint i=1;i <= N;++i) {
            out<<ans[i]<<' ';
        }

        //ans[1] ^= 1;
        for (zint i=1;i <= M;++i) {
            if (!eval(i)) {
                cout<<"BAD\n";
                break;
            }
        }

        /*
        for (zint c=1;c <= nrComp;++c) {
            getEdges(c);
        }
        //*/
    }
    else {
        out<<"-1\n";
    }

    in.close();out.close();
    return 0;
}

void getStack(zint node) {
    visStack[node] = true;

    for (zint nxt : vEdge[node]) {
        if (visStack[nxt]) {
            continue;
        }

        getStack(nxt);
    }

    st.push(node);
}

void getComp(zint node) {
    comp[nrComp].pb(node);
    compOf[node] = nrComp;
    inComp[node] = true;

    for (zint nxt : vRev[node]) {
        if (inComp[nxt]) {
            continue;
        }

        getComp(nxt);
    }
}

void update(zint c,const zint val) {
    if (visComp[c]) {
        return;
    }

    zint x = comp[c][0],
         invx = (x > N) ? x-N : x+N,
         comp2 = compOf[invx];
    visComp[c] = true;
    visComp[comp2] = true;
    for (auto node : comp[c]) {
        zint temp = val;
        if (node > N) {
            node -= N;
            temp ^= 1;
        }
        ans[node] = temp;
    }
}

void getEdges(zint c) {
    bool vis[2*NMax] = {};
    vis[c] = true;

    for (zint node : comp[c]) {
        for (zint nxt : vEdge[node]) {
            nxt = compOf[nxt];
            if (vis[nxt]) {
                continue;
            }

            vis[nxt] = true;
            vComp[c].pb(nxt);
        }
    }
}