Cod sursa(job #2331889)

Utilizator gabiluciuLuciu Gabriel gabiluciu Data 30 ianuarie 2019 09:19:08
Problema 2SAT Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.82 kb
/*
ID: gabriel100
LANG: C++
TASK:
*/
#include <cstdio>
#include <algorithm>
//#include <time.h>
#include <queue>
#include <cmath>
#include <stack>
#include <fstream>
#include <bitset>
#include <set>
#include <map>
#include <ctime>
#include <list>
#include <cstring>

#define nl '\n'
#define F first
#define S second
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define eb(x) emplace_back(x)
#define ull unsigned long long
#define ll long long
#define ProblemName "2sat"
#define LocalName "data"
#ifdef INFOARENA
#define Filename ProblemName
#else
#define Filename LocalName
#endif
#define Input Filename".in"
#define Output Filename".out"
using namespace std;
ifstream cin(Input);
ofstream cout(Output);

template<class a, class type>
void print(a v, type t) {
    for_each(all(v), [](type x) { cout << x << ' '; });
    cout << nl;
}

struct nod {
    vi v1;
    vi v2; // invers
};
int n, m;
nod v[2 * 100000 + 1];

inline void add(const int &ind, const int &val) {
    v[ind].v1.emplace_back(val);
    v[val].v2.emplace_back(ind); //inv
}

bitset<2 * 100001> viz;
stack<int> s;

inline void dfs(int i) {
    viz[i] = true;
    for (auto it = v[i].v1.begin(); it != v[i].v1.end(); ++it) {
        if (!viz[*it]) {
            dfs(*it);
        }
    }
    s.push(i);
}

int grup[2 * 100001];
int nrGrup = 1;

vector<vi > ans;

inline void dfs_inv(int i) {
    viz[i] = true;
    for (auto it = v[i].v2.begin(); it != v[i].v2.end(); ++it) {
        if (!viz[*it]) {
            dfs_inv(*it);
        }
    }
    ans[nrGrup].eb(i);
//    if (i > n)
//        ans[nrGrup - 0b1].eb(i - n);
//    else
//        ans[nrGrup - 0b1].eb(-i);
    grup[i] = nrGrup;
}

bitset<200002> aparut;

bool verif(int nr) {
    return aparut[nr];
}

int main() {
    ios_base::sync_with_stdio(false);
    clock_t tStart = clock();
    cin >> n >> m;
    int x1, x2;
    for (int j = 0; j < m; ++j) {
        cin >> x1 >> x2;
        if(x1>0)
        aparut[x1+n] = true;
        else aparut[-x1] = true;
        if(x2>0)
        aparut[x2+n] = true;
        else aparut[-x2] = true;
        if (x1 < 0 && x2 < 0) { // ambele negata
            add(-x1 + n, -x2); // x1 -> ~x2    1,2,3 .. n sunt negate   n+1, n+2 .. n+n=2*n NU sunt negate
            add(-x2 + n, -x1); // x2 -> ~x1
        } else if (x1 > 0 && x2 > 0) {
            add(x1, x2 + n); // ~x1 -> x2
            add(x2, x1 + n); // ~x2 -> x1
        } else if (x1 < 0 && x2 > 0) {
            add(-x1 + n, x2 + n); // x1 -> x2     ~~x1 = x1
            add(x2, -x1);     // ~x2 -> ~x1
        } else if (x1 > 0 && x2 < 0) {
            add(x1, -x2);
            add(-x2 + n, x1 + n);
        }
    }
    for (int i = 1; i <= n * 2; ++i) {
        if (!viz[i]) {
            dfs(i);
        }
    }

    for (int i = 0; i <= 2 * n; ++i)viz[i] = false; // foarte important:: aici ESTE 2*N  !!!!!!!!!!
    vi temp;
    ans.eb(temp);
    while (!s.empty()) {
        if (!viz[s.top()]) {
            ans.emplace_back(temp);
            dfs_inv(s.top());
            ++nrGrup;
        }
        s.pop();
    }
    // verificare
    int nuExistaSolutie = -1;
    for (int i = 1; i <= n; ++i) {
        if (grup[i] == grup[i + n]) { // nrGrup incepe de la 1
            cout << nuExistaSolutie << nl;
            return 0;
        }
    }
    vector<int> rez(2 * n + 1, -1);
    for (int it = ans.size() - 1; it >= 0; --it) {
        for (auto it2 = ans[it].begin(); it2 != ans[it].end(); ++it2) {
            if (rez[*it2] == -1 && verif(*it2)) {
                rez[*it2] = 1;
                if (*it2 <= n) {
                    rez[*it2 + n] = 0;
                } else {
                    rez[*it2 - n] = 0;
                }
            }
        }
    }

    for (int i = 1; i <= n; ++i)
//        if (aparut[i+n])
            cout << rez[i + n] << ' ';

    cout.close();
    printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}