Cod sursa(job #3289455)

Utilizator Allie28Radu Alesia Allie28 Data 26 martie 2025 22:08:21
Problema 2SAT Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.96 kb
#include <bits/stdc++.h>

#define ll long long

using namespace std;

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

const int LMAX = 200005;
const int INF = 0x3f3f3f3f;
const ll MOD = 1000000007;

//xi --> i, !xi --> i + n
int comp, ind;
bool viz[LMAX], val[LMAX];
int root[LMAX];
vector<int> order, L[LMAX], LT[LMAX], alc[LMAX], G[LMAX];

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

void dfs(int node) {
    viz[node] = 1;
    for (int vec : L[node]) {
        if (!viz[vec]) dfs(vec);
    }
    order.push_back(node);
}

void dfs2(int node) {
    root[node] = comp;
//    fout<<node<<" ";
    alc[comp].push_back(node);
    for (int vec : LT[node]) {
        if (!root[vec]) {
            dfs2(vec);
        }
    }
}


int main() {
    int n, m, a, b, i;
    fin>>n>>m;
    ind = n;
    while (m--) {
        fin>>a>>b;
        //implicatii
        a = -a;
//        fout<<build(a)<<" "<<build(b)<<endl;
        L[build(a)].push_back(build(b));
        LT[build(b)].push_back(build(a));
        //contrapozitiva
        a = -a;
        b = -b;
//        fout<<build(b)<<" "<<build(a)<<endl;
        L[build(b)].push_back(build(a));
        LT[build(a)].push_back(build(b));
    }
    n = n*2;
    for (i = 1; i <= n; i++) {
        if (!viz[i]) {
            dfs(i);
        }
    }
    //sortare top
    reverse(order.begin(), order.end());
    comp = 1;
    for (i = 0; i < n; i++) {
//        fout<<order[i]<<" ";
        if (!root[order[i]]) {
            dfs2(order[i]);
//            fout<<endl;
            comp++;
        }
        if (root[order[i]] == root[build(-order[i])]) {
            fout<<-1;
            return 0;
        }
        viz[i + 1] = 0;
    }

    for (i = 1; i <= n/2; i++) {
        if (root[i] > root[i + ind]) {
            fout<<1;
        }
        else fout<<0;
        fout<<" ";
    }




    fin.close();
    fout.close();
    return 0;
}