Cod sursa(job #1688288)

Utilizator xtreme77Patrick Sava xtreme77 Data 13 aprilie 2016 13:17:27
Problema 2SAT Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.29 kb
/**
 * Code by Patrick Sava
 * "Spiru Haret" National College of Bucharest
 **/

# include "fstream"
# include "cstring"
# include "vector"
# include "queue"
# include "bitset"
# include "algorithm"
# include "map"
# include "set"
# include "unordered_map"
# include "deque"
# include "string"
# include "iomanip"
# include "cmath"
# include "stack"
# include "cassert"

const char IN [ ] =  "2sat.in" ;
const char OUT [ ] = "2sat.out" ;

using namespace std ;

# define pb push_back
# define mp make_pair
# define FORN( a , b , c ) for ( register int a = b ; a <= c ; ++ a )
# define FORNBACK( a , b , c ) for ( register int a = b ; a >= c ; -- a )

ifstream cin ( IN ) ;
ofstream cout ( OUT ) ;

const int MAX = 200270 ;

int n ;

int opus ( int nod )
{
    if ( nod > n ) {
        return nod - n ;
    }
    else {
        return nod + n ;
    }
}

vector < int > gr [ MAX ] ;
vector < int > inv [ MAX ] ;

bitset < MAX > viz ;

vector < int > lista ;

inline void dfs ( int nod )
{
    viz [ nod ] = 1 ;
    for ( auto x : gr [ nod ] )
    {
        if ( viz [ x ] == 0 ) {
            dfs ( x ) ;
        }
    }
    lista.pb ( nod ) ;
}

bool solution [ MAX ] ;

inline void dfs2 ( int nod )
{
    if ( solution [ nod ] == 1 ) {
        cout << -1 << '\n' ;
        exit ( 0 ) ;
    }
    viz [ nod ] = 0 ;
    solution [ nod ] = 0 ;
    solution [ opus ( nod ) ] = 1 ;
    for ( auto x : inv [ nod ] ) {
        if ( viz [ x ] ) {
            dfs2 ( x ) ;
        }
    }
}

int main()
{
    int m ;
    cin >> n >> m ;
    while ( m -- )
    {
        int x , y ;
        cin >> x >> y ;
        if ( x < 0 ) {
            x = n - x ;
        }
        if ( y < 0 ) {
            y = n - y ;
        }
        gr [ opus ( x ) ].pb ( y ) ;
        gr [ opus ( y ) ].pb ( x ) ;
        inv [ x ].pb ( opus ( y ) ) ;
        inv [ y ].pb ( opus ( x ) ) ;
    }
    FORN ( i , 1 , 2 * n ) {
        if ( viz [ i ] == 0 ) {
            dfs ( i ) ;
        }
    }
    reverse ( lista.begin() , lista.end() ) ;
    for ( auto x : lista )
    {
        if ( viz [ x ] and viz [ opus ( x ) ] ) {
            dfs2 ( x ) ;
        }
    }
    FORN ( i , 1 , n ) {
        cout << solution [ i ] << ' ' ;
    }
    return 0;
}