Cod sursa(job #1944012)

Utilizator CodrutLemeniCodrut Lemeni CodrutLemeni Data 28 martie 2017 21:58:54
Problema Andrei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.45 kb
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <stack>

using namespace std;

const int N = 200100;

int noNumbers , noEdges ;

vector < int > muc [ N ];
stack < int > stk ;
int instk [ N ] ;
int low [ N ] , preord [ N ] , cr = 1;
int viz [ N ] , val [ N ];

void CalcTarjan ( int node ){
    vector < int >:: iterator it ;

    stk.push( node );
    instk [ node ] = 1 ;
    viz [ node ] = 1 ;
    preord [ node ] = low [ node ] = cr ++ ;

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

        if ( viz [ *it ] == 0 ){
            CalcTarjan( *it );
            low [ node ] = min ( low [ node ] , low [ *it ] );
        }else{
            if ( instk [ *it ] ){
                low [ node ] = min ( low[ node ] , low [ *it ] );
            }

        }
    }

    if ( low [ node ] == preord [ node ] ){
        int x ;
        do {
            x = stk.top() ;
            stk.pop();
            instk [ x ] = 0 ;

            if ( val [ x ] ==  -1  ){
                if ( x > noNumbers ){
                    val [ x - noNumbers ] = 1 ;
                    val [ x  ] = 1 ;
                }else{
                    val [ x ] = 0 ;
                    val [ x + noNumbers ] = 0 ;
                }
            }

        }while ( x != node );
    }
}


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

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

    scanf("%d %d", &noNumbers , &noEdges );

    memset ( val , -1 , sizeof val );

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

        xnot = x + noNumbers ;
        ynot = y + noNumbers ;

        switch ( type ){
        case 0 :
            muc [ xnot ].push_back( y );
            muc [ ynot ].push_back( x );
            break;
        case 1 :
            muc [ x ].push_back( ynot );
            muc [ y ].push_back( xnot );
            break;
        case 2 :
            muc [ x ].push_back( y );
            muc [ y ].push_back( x );
            muc [ xnot ].push_back( ynot );
            muc [ ynot ].push_back( xnot );
            break ;
        }

    }


    for ( i = 1 ; i <= 2 * noNumbers ; i++ ){
        if ( !viz [ i ] ){
            CalcTarjan ( i );
        }

    }

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

    return 0;
}