Cod sursa(job #2668588)

Utilizator ReksioCroftOctavian Florin Staicu ReksioCroft Data 5 noiembrie 2020 02:08:03
Problema Componente biconexe Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 5.15 kb
#include <cstdio>
#include <cassert>
#include <cctype>
#include <vector>

using namespace std;
const int nMax = 100001;
vector< int > v[nMax];
int nivMin[nMax];
int niv[nMax];
vector< vector< int > > componente;

class inputParser {

    static const unsigned int buffSize = 1 << 17;
    unsigned int pozBuff;
    FILE *fin;
    unsigned char *buffer;


    void getChar( unsigned char &ch ) {
        if ( pozBuff == buffSize ) {
            pozBuff = 0;
            assert( fread( buffer, sizeof( char ), buffSize, fin ) );
        }
        ch = buffer[ pozBuff++ ];
    }


public:
    explicit inputParser( const char *fileName ) {
        fin = fopen( fileName, "r" );
        assert( fin != nullptr );
        buffer = new unsigned char[buffSize];
        pozBuff = buffSize;
    }


    inputParser( const inputParser &dummy ) = delete;

    inputParser &operator=( const inputParser &dummy ) = delete;


    template < class intType >
    inputParser &operator>>( intType &nr ) {
        int sgn( 1 );
        unsigned char ch;
        nr = 0;
        getChar( ch );
        while ( isdigit( ch ) == 0 && ch != '-' )
            getChar( ch );
        if ( ch == '-' ) {
            sgn = -1;
            getChar( ch );
        }
        while ( isdigit( ch ) != 0 ) {
            nr = nr * 10 + ch - '0';
            getChar( ch );
        }
        nr *= sgn;
        return *this;
    }


    inputParser &operator>>( char &ch ) {
        unsigned char ch2;
        do {
            getChar( ch2 );
        } while ( isgraph( ch2 ) == 0 );
        ch = static_cast<char>(ch2);
        return *this;
    }


    inputParser &operator>>( unsigned char &ch ) {
        getChar( ch );
        do {
            getChar( ch );
        } while ( isgraph( ch ) == 0 );
        return *this;
    }


    ~inputParser() {
        fclose( fin );
        delete[] buffer;
    }

};

class outputParser {

    static const unsigned int buffSize = 1 << 17;
    unsigned int pozBuff;
    FILE *fout;
    unsigned char *buffer;


    void putChar( const unsigned char ch ) {
        if ( pozBuff == buffSize ) {
            pozBuff = 0;
            fwrite( buffer, sizeof( char ), buffSize, fout );
        }
        buffer[ pozBuff++ ] = ch;
    }


public:
    explicit outputParser( const char *fileName ) {
        fout = fopen( fileName, "w" );
        buffer = new unsigned char[buffSize];
        pozBuff = 0;
    }


    outputParser( const inputParser &dummy ) = delete;

    outputParser &operator=( const inputParser &dummy ) = delete;


    template < class intType >
    outputParser &operator<<( intType &nr ) {
        if ( nr < 0 ) {
            putChar( '-' );
            nr = -nr;
        }
        int cif[20];
        int pozCif = 0;
        do {
            cif[ pozCif++ ] = nr % 10;
            nr /= 10;
        } while ( nr > 0 );
        while ( pozCif > 0 ) {
            unsigned char ch = cif[ --pozCif ] + '0';
            putChar( ch );
        }
        return *this;
    }


    outputParser &operator<<( char ch ) {
        unsigned char ch2 = static_cast<unsigned char>(ch2);
        putChar( ch );
        return *this;
    }


    outputParser &operator<<( unsigned char ch ) {
        putChar( ch );
        return *this;
    }


    ~outputParser() {
        fwrite( buffer, sizeof( char ), pozBuff, fout );
        delete[] buffer;
        fclose( fout );
    }

};

vector< int > dfs( int tata, int nodCurent, int nivel ) {
    niv[ nodCurent ] = nivMin[ nodCurent ] = nivel;
    vector< int > compActuala = { nodCurent };

    for ( auto &i:v[ nodCurent ] ) {
        if ( i != tata ) {
            if ( niv[ i ] == 0 ) {
                vector< int > subComponenta = dfs( nodCurent, i, nivel + 1 );
                compActuala.insert( compActuala.end(), subComponenta.begin(), subComponenta.end() );
            }

            if ( niv[ i ] < niv[ nodCurent ] ) {
                if ( niv[ i ] < nivMin[ nodCurent ] )
                    nivMin[ nodCurent ] = niv[ i ];
            }
            else {
                if ( nivMin[ i ] < nivMin[ nodCurent ] )
                    nivMin[ nodCurent ] = nivMin[ i ];
            }
        }
    }

    if ( nivMin[ nodCurent ] >= nivel - 1 ) {
        if ( nivMin[ nodCurent ] == nivel - 1 )
            compActuala.push_back( tata );
        if ( tata > 0 && compActuala.size() == 1 )
            compActuala.push_back( tata );
        if ( compActuala.size() > 1 )
            componente.emplace_back( compActuala );
        return vector< int >{};
    }
    else
        return compActuala;
}

int main() {
    int n, m;
    //FILE *fout = fopen( "biconex.out", "w" );
    inputParser fin( "biconex.in" );
    outputParser fout( "biconex.out" );
    fin >> n >> m;
    for ( int i = 0; i < m; i++ ) {
        int a, b;
        fin >> a >> b;
        v[ a ].push_back( b );
        v[ b ].push_back( a );
    }

    dfs( 0, 1, 1 );

//    fprintf( fout, "%zu\n", componente.size() );
    int nr = componente.size();
    fout << nr << '\n';
    for ( auto &i : componente ) {
        for ( auto &j:i ) {
            // fprintf( fout, "%d ", j );
            fout << j << ' ';
        }
        // fputc( '\n', fout );
        fout << '\n';
    }
    //fclose( fout );
    return 0;
}