Cod sursa(job #2941735)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 18 noiembrie 2022 10:26:09
Problema Andrei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.42 kb
#include <stdio.h>
#include <ctype.h>

#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

static inline int min( const int& a, const int& b ) {
    return ( a <= b ? a : b );
}
 
static inline int max( const int& a, const int& b ) {
    return ( a >= b ? a : b );
}

static inline void swap( int &a, int &b ) {
    a ^= b;
    b ^= a;
    a ^= b;
}

const int MAXBUFF = ( 1 << 9 );
FILE *fin;

int pozBuff;
char buff[ MAXBUFF ];
inline char nextChar() {
    if( pozBuff == MAXBUFF ) {
        fread( buff, 1, MAXBUFF, fin );
        pozBuff = 0;
    }
 
    return buff[ pozBuff++ ];
}
 
static int no, ch;
inline int readInt() {
    no = 0;
 
    while( !isdigit( ch = nextChar() ) );
    do
        no = no * 10 + ch - '0';
    while( isdigit( ch = nextChar() ) );
 
    return no;
}
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector>

const int MAX = 100005; 

std::vector<int> noduri[ MAX * 2 ];
bool viz[ MAX * 2 ];
int s[ MAX * 2 ];
int r[ MAX * 2 ];
int n, m, top;

inline void muchie( int u, int v ) {
    noduri[ -u + n ].push_back( v + n );
    noduri[ -v + n ].push_back( u + n );
}

void dfs( int u ) {
    viz[ u ] = 1;
    for( int v : noduri[ u ] )
        if( !viz[ v ] )
            dfs( v );
    s[ ++top ] = u;
}

int main() 
{
    { // read and make graph
        fin = fopen( "andrei.in", "r" );
        fread( buff, 1, MAXBUFF, fin );
        
        n = readInt();
        m = readInt();
        int u, v, col;
        for( int i = 0; i < m; i++ ) {
            u = readInt();
            v = readInt();
            col = readInt();

            if( col == 0 )
                muchie( u, v );
            else if( col == 1 )
                muchie( -u, -v );
            else { // col == 2
                muchie( u, -v );
                muchie( -u, v );
            }
        }

        fclose( fin );
    }


    int N = ( n << 1 );
    for( int i = 0; i <= N; i++ )
        if( !viz[ i ] )
            dfs( i );

    int u, v;
    do {
        u = s[ top ] - n;
        v = -u;

        u += n;
        v += n;

        if( !r[ u ] && !r[ v ] )
            r[ v ] = 1;
    } while( --top );


    { // afisare
        FILE *fout = fopen( "andrei.out", "w" );
        for( int i = n + 1; i <= N; i++ )
            fprintf( fout, "%d ", r[ i ] );
        fputc( '\n', fout );
        fclose( fout );
    }
    return 0;
}