Cod sursa(job #1652121)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 14 martie 2016 17:58:21
Problema Andrei Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include<cstdio>
#include<vector>
#include<cstring>

using namespace std;

FILE *fin = fopen( "andrei.in", "r" ), *fout = fopen( "andrei.out", "w" );

typedef vector< int > graf;

const int nmax = 1e5;
int n, nr_comp;
bool viz[ 2 * nmax + 1 ];
int c[ 2 * nmax + 1 ];
graf g[ 2 * nmax + 1 ][ 2 ];

vector< int > ord;

inline int negat( int x ) {
    return ( x <= n ? x + n : x - n );
}
void muchie( int x, int y ) {
    g[ negat( x ) ][ 0 ].push_back( y );
    g[ negat( y ) ][ 0 ].push_back( x );

    g[ x ][ 1 ].push_back( negat( y ) );
    g[ y ][ 1 ].push_back( negat( x ) );
}
void dfs( int nod, int ind ) {
    viz[ nod ] = 1;
    for( graf::iterator it = g[ nod ][ ind ].begin(); it != g[ nod ][ ind ].end(); ++ it ) {
        if ( viz[ *it ] == 0 ) {
            dfs( *it, ind );
        }
    }
    if ( ind == 0 ) {
        ord.push_back( nod );
    } else {
        c[ nod ] = nr_comp;
    }
}
void plus_minus() {
    for( int i = 1; i <= 2 * n; ++ i ) {
        if ( viz[ i ] == 0 ) {
            dfs( i, 0 );
        }
    }
    memset( viz, 0, sizeof( viz ) );
    for( int i = ord.size() - 1; i >= 0; -- i ) {
        if ( viz[ ord[ i ] ] == 0 ) {
            ++ nr_comp;
            dfs( ord[ i ], 1 );
        }
    }
}
int main() {
    int m;
    fscanf( fin, "%d%d", &n, &m );
    for( int i = 0; i < m; ++ i ) {
        int x, y, z;
        fscanf( fin, "%d%d%d", &x, &y, &z );
        switch( z ) {
            case 0 : muchie( x, y ); break;
            case 1 : muchie( negat( x ), negat( y ) ); break;
            default : muchie( x, negat( y ) ); break;
        }
    }

    plus_minus();

    for( int i = 1; i <= n; ++ i ) {
        if ( c[ i ] < c[ negat( i ) ] ) {
            fprintf( fout, "0 " );
        } else {
            fprintf( fout, "1 " );
        }
    }
    fprintf( fout, "\n" );
    fclose( fin );
    fclose( fout );
    return 0;
}