Cod sursa(job #1934255)

Utilizator CodrutLemeniCodrut Lemeni CodrutLemeni Data 21 martie 2017 12:13:01
Problema 2SAT Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 2.89 kb

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <set>

using namespace std;

const int N = 200010;

int noVar , noOper ;

int preOrd [ N ] , low [ N ] , inStack [ N ] , varValues[ N ]  ;
int nrCr =1 , spySol ;

vector < int > edges[ N ];
stack < int > stk;
set < int > el ;


void CalcTarjan ( int node ){
    int vec  ;
    vector < int > :: iterator it ;
    static int value ,nodecmp ;

    preOrd [ node ] = low[ node ] = nrCr ++ ;
    stk.push( node );
    inStack [ node ] = 1 ;

    for ( it = edges [ node ].begin() ; it != edges[ node ].end() ; it++ ){

        vec = *it ;
        if ( preOrd [ vec ] == 0 ){

            CalcTarjan( vec );
            low [ node ] = min( low [ node ] , low[ vec ] );

        }else{
            if ( inStack [ vec ] ){
                low [ node ] = min ( low[ node ] , low[ vec ] );
            }
        }

    }

    if ( low [ node ] == preOrd [ node ] ){

        nodecmp = node ;
        if ( node > noVar ){
            nodecmp = node - noVar ;
        }
        el.clear();
        do {
            vec = stk.top();
            stk.pop();

            el.insert( vec );
            if ( vec > noVar ){
                if ( el.find( vec - noVar) != el.end() ){
                    spySol = 1 ;
                }
            }else{
                if ( el.find( vec + noVar) != el.end() ){
                    spySol = 1 ;
                }
            }

            inStack [ vec ] = 0 ;

            value = 0 ;
            if ( vec <= noVar ){
                value = 1 ;
            }

            if ( vec > noVar ){
                vec -= noVar ;
            }

            if ( varValues[ vec ] == -1 ){
                varValues [ vec ] = value ;
            }
        }while ( vec != nodecmp );
    }

}



int main(){
    int x , y , xnot , ynot ;
    int i ;

    freopen("2sat.in","r",stdin);
    freopen("2sat.out","w",stdout);


    scanf("%d %d",&noVar , &noOper );


    memset ( varValues , -1 , sizeof varValues ) ;

    for ( i = 0 ; i < noOper ; i ++ ){
        scanf("%d %d",&x , &y );

        if( x < 0 ){
            xnot = abs( x ) ;
            x = xnot + noVar ;
        }else{
            xnot = x + noVar ;
        }

        if( y < 0 ){
            ynot = abs( y ) ;
            y = ynot + noVar ;
        }else{
            ynot = y + noVar ;
        }

        edges[ xnot ].push_back ( y );
        edges[ ynot ].push_back ( x );

    }


    for ( i = 1 ; i <= 2 * noVar ; i ++ ){
        if ( preOrd [ i ] ){
            continue ;
        }
        CalcTarjan( i );
    }

    if ( spySol == 1 ){
        printf("-1");
        return 0 ;
    }

    for ( i = 1 ; i <= noVar ; i ++ ){
        printf("%d ",varValues [ i  ]);
    }

    return 0;
}