Cod sursa(job #3041404)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 31 martie 2023 14:00:21
Problema 2SAT Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

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

const int max_size = 2e5 + 1;

int ctc[max_size], viz[max_size], n;
vector <int> mc[max_size], topsort, invmc[max_size];

void dfs (int nod)
{
    viz[nod] = 1;
    for (auto f : mc[nod])
    {
        if (!viz[f])
        {
            dfs(f);
        }
    }
    topsort.push_back(nod);
}

void kos (int nod, int ct)
{
    viz[nod] = 0;
    ctc[nod] = ct;
    for (auto f : invmc[nod])
    {
        if (viz[f])
        {
            kos(f, ct);
        }
    }
}

int neg (int x)
{
    if (x > n)
    {
        return x - n;
    }
    return x + n;
}

int main ()
{
    int m;
    in >> n >> m;
    while (m--)
    {
        int x, y;
        in >> x >> y;
        if (x < 0)
        {
            x = -x + n;
        }
        if (y < 0)
        {
            y = -y + n;
        }
        mc[neg(x)].push_back(y);
        mc[neg(y)].push_back(x);
        invmc[x].push_back(neg(y));
        invmc[y].push_back(neg(x));
    }
    for (int i = 1; i <= 2 * n; i++)
    {
        if (viz[i] == 0)
        {
            dfs(i);
        }
    }
    reverse(topsort.begin(), topsort.end());
    int ct = 0;
    for (auto f : topsort)
    {
        if (viz[f])
        {
            ++ct;
            kos(f, ct);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (ctc[i] == ctc[i + n])
        {
            out << -1;
            return 0;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (ctc[i] < ctc[i + n])
        {
            out << 1 << " ";
        }
        else
        {
            out << 0 << " ";
        }
    }
    in.close();
    out.close();
    return 0;
}