Cod sursa(job #456491)

Utilizator alexandru92alexandru alexandru92 Data 15 mai 2010 18:43:21
Problema Componente biconexe Scor 94
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on May 15, 2010, 5:13 PM
 */
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 100011

/*
 *
 */
using namespace std;
typedef pair< int, int > pr;
int N, nrc, ii;
int idx[Nmax], ll[Nmax];
stack< pr > S;
vector< int > G[Nmax], Bcc[Nmax];
inline void GetBcc( pr x )
{
    static pr y;
    vector< bool > uz(N);
    ++nrc;
    do
    {
        y=S.top(); S.pop();
        if( !uz[y.first] )
        {
            uz[y.first]=true;
            Bcc[nrc].push_back(y.first);
        }
        if( !uz[y.second] )
        {
            uz[y.second]=true;
            Bcc[nrc].push_back(y.second);
        }
    }while( y != x );
}
inline void DFS( int x )
{
    if( G[x].empty() )
        return;
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    idx[x]=ll[x]=++ii;
    for( ; it < iend; ++it )
    {
        if( 0 == idx[*it] )
        {
            S.push( pr( x, *it ) );
            DFS(*it);
            ll[x]=min( ll[x], ll[*it] );
            if( ll[*it] >= idx[x] )
                GetBcc( pr( x, *it ) );
        }
        else ll[x]=min( ll[x], idx[*it] );
    }
}
int main( void )
{
    int M, x, y;
    ifstream in( "biconex.in" );
    for( in>>N>>M; M; --M )
    {
        in>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for( x=1; x <= N; ++x )
        if( 0 == idx[x] )
            DFS(x);
    ofstream out( "biconex.out" );
    out<<nrc<<'\n';
    for( x=1; x <= nrc; ++x )
    {
        copy( Bcc[x].begin(), Bcc[x].end(), ostream_iterator<int>( out, " " ) );
        out<<'\n';
    }
    return EXIT_SUCCESS;
}