Cod sursa(job #3304969)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 29 iulie 2025 10:47:50
Problema 2SAT Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <unordered_map>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

const string txt = "2sat";
const int nmax = 2e5 + 5;

ifstream f(txt + ".in");
ofstream g(txt + ".out");

int n, m, k, c[nmax], fr[nmax];
vector<int> v[5][nmax], aux;

static void dfs(int nod, int tip)
{
    fr[nod] = tip + 1;
    for (auto x : v[tip][nod])
        if (fr[x] != tip + 1)
            dfs(x, tip);

    if (!tip) aux.push_back(nod);
    else c[nod] = k;
}

int main()
{
    f >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y; f >> x >> y;
        v[0][-x + n].push_back(y + n);
        v[0][-y + n].push_back(x + n);

        v[1][x + n].push_back(-y + n);
        v[1][y + n].push_back(-x + n);
    }

    for(int i = 1; i <= 2 * n; i ++)
        if(!fr[i])
            dfs(i, 0);
    
    for (int i = aux.size() - 1; i >= 0; i--)
        if (fr[aux[i]] != 2)
            k++, dfs(aux[i], 1);

    int oki = 1;
    for (int i = 1; i <= n; i++)
        if (c[i + n] == c[-i + n]) oki = 0;

    if (!oki) {
        g << -1;
        return 0;
    }

    for (int i = 1; i <= n; i++)
        g << (c[i + n] > c[-i + n]) << " ";
    return 0;
}