Cod sursa(job #2949572)

Utilizator Fantastic_Mantudor voicu Fantastic_Man Data 1 decembrie 2022 00:30:32
Problema 2SAT Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
const int nmax = 1e5;

vector < int > g[2 * nmax + 1], gt[2 * nmax + 1];
bool vis[2 * nmax + 1];
vector < int > kosaraju;
int comp[2 * nmax + 1];
int n;

int Not ( int x ) {
    if ( x <= n )
        return x + n;
    return x - n;
}

void add_implication ( int x, int y ) {
    g[x].push_back ( y );
    gt[y].push_back ( x );
}

int ctc = 0;
void dfs ( int node ) {
    vis[node] = true;
    comp[node] = ctc;
    for ( int x: g[node] )
        if ( vis[x] == false )
            dfs ( x );
    kosaraju.push_back ( node );
}


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

int main() {
    int m, x, y;

    fin >> n >> m;
    for ( int i = 0; i < m; i++ ) {
        fin >> x >> y;
        if ( x < 0 )
            x = -x + n;
        if ( y < 0 )
            y = -y + n;
        add_implication ( Not ( x ), y );
        add_implication ( Not ( y ), x );
    }

    for ( int i = 1; i <= 2 * n; i++ )
        if ( vis[i] == false )
            dfs ( i );

    reverse ( kosaraju.begin (), kosaraju.end () );

    swap ( g, gt );
    for ( int i = 1; i <= 2 * n; i++ )
        vis[i] = false;

    ctc = 0;
    for ( int x: kosaraju )
        if ( vis[x] == false )
            ctc++, dfs ( x );

    int i = 1;
    while ( i <= n && comp[i] != comp[i + n] )
        i++;

    if ( i > n ) {
        for ( int i = 1; i <= n; i++ )
            fout << ( comp[i] > comp[i + n] ) << ' ';
    } else
        fout << -1;

    return 0;
}