Cod sursa(job #2672083)

Utilizator ReksioCroftOctavian Florin Staicu ReksioCroft Data 13 noiembrie 2020 00:03:14
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.75 kb
#include <cstdio>
#include <cassert>
#include <cctype>
#include <vector>
#include <bitset>

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 outputParser &dummy ) = delete;

    outputParser &operator=( const outputParser &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 ) {
        putChar( ch );
        return *this;
    }


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


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

};

using namespace std;
const int nMax = 100001;
inputParser fin( "biconex.in" );
outputParser fout( "biconex.out" );
int level[nMax];
int hmin[nMax];
int p[nMax];
vector< int > v[nMax];
vector< int > tree[nMax];
vector< int > compBiconexa[nMax];
bitset< nMax > vizitat;

void dfs( int nod, int nivel ) {
    level[ nod ] = hmin[ nod ] = nivel;
    for ( auto &i : v[ nod ] ) {

        if ( level[ i ] == 0 ) {
            dfs( i, nivel + 1 );

            if ( hmin[ i ] < nivel ) {
                tree[ i ].push_back( nod );
                tree[ nod ].push_back( i );
            }
            else
                p[ i ] = nod;

            hmin[ nod ] = min( hmin[ nod ], hmin[ i ] );
        }
        else
            hmin[ nod ] = min( hmin[ nod ], level[ i ] );

    }
}

void biconex( int nod, int nrcomp ) {
    vizitat[ nod ] = true;
    compBiconexa[ nrcomp ].push_back( nod );
    if ( p[ nod ] )
        compBiconexa[ nrcomp ].push_back( p[ nod ] );
    for ( auto &i:tree[ nod ] ) { ///ce face parte din componenta actuala
        if ( !vizitat[ i ] )
            biconex( i, nrcomp );
    }
}

int main() {
    int n, m, co( 0 );
    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( 1, 1 );
    for ( int i = 2; i <= n; i++ ) {
        if ( !vizitat[ i ] ) {
            biconex( i, ++co );
        }
    }

    fout << co << '\n';

    for ( int i = 1; i <= co; i++ ) {
        for ( auto &nod : compBiconexa[ i ] ) {
            fout << nod << ' ';
        }
        fout << '\n';
    }
    return 0;
}