Cod sursa(job #3240214)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 13 august 2024 11:01:33
Problema 2SAT Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.93 kb
#include <bits/stdc++.h>

const std :: string FILENAME = "2sat";

std :: ifstream in (FILENAME + ".in");

std :: ofstream out (FILENAME + ".out");

const int NMAX = 1e5 + 5;

int n;

int m;

int x;

int y;

int nx;

int ny;

int cnt;

int comp[NMAX];

int val[NMAX];

std :: vector <int> v[NMAX];

std :: vector <int> t[NMAX];

std :: bitset <NMAX> visited;

std :: vector <int> tout;

void dfs(int nod)
{
    for(int i : v[nod])
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs(i);
        }
    }

    tout.push_back(nod);
}

void dfs1(int nod)
{
    comp[nod] = cnt;

    for(int i : t[nod])
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs1(i);
        }
    }
}

int main()
{

    in >> n >> m;

    for(int i = 1; i <= m; i ++)
    {
        in >> x >> y;

        x = (x > 0) ? x : (-x + n);

        y = (y > 0) ? y : (-y + n);

        nx = (x <= n) ? (x + n) : (x - n);

        ny = (y <= n) ? (y + n) : (y - n);

        v[nx].push_back(y);

        t[y].push_back(nx);

        v[ny].push_back(x);

        t[x].push_back(ny);
    }

    for(int i = 1; i <= 2 * n; i ++)
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs(i);
        }
    }

    std :: reverse(tout.begin(), tout.end());

    visited &= 0;

    for(int i : tout)
    {
        if(visited[i] == false)
        {
            cnt ++;

            visited[i] = true;

            dfs1(i);
        }
    }

    for(int i = 1; i <= n; i ++)
    {
        if(comp[i] == comp[i + n])
        {
            out << -1;

            return 0;
        }

        if(comp[i] > comp[i + n])
        {
            val[i] = 1;
        }
    }

    for(int i = 1; i <= n; i ++)
    {
        out << val[i] << " ";
    }

    return 0;
}