Cod sursa(job #456469)

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

/*
 * 
 */
using namespace std;
typedef pair< int, int > pr;
int nrc, ii;
int idx[Nmax], lowlink[Nmax];
stack< pr > S;
bitset< Nmax > uz;
vector< int > G[Nmax], BCC[Nmax];
inline void GetBCC( pr x )
{
    static pr y;
    ++nrc;
    do
    {
        y=S.top(); S.pop();
        if( ! uz.test(y.first) )
        {
            uz.set(y.first);
            BCC[nrc].push_back(y.first);
        }
        if( !uz.test(y.second) )
        {
            uz.set(y.second);
            BCC[nrc].push_back(y.second);
        }
    }while(  y != x );
    uz&=0;
}
inline void DFS( int x )
{
    vector< int >::iterator it=G[x].begin(), iend=G[x].end();
    idx[x]=lowlink[x]=++ii;
    for( ; it < iend; ++it )
    {
        if( 0 == idx[*it] )
        {
            S.push( pr( x, *it ) );
            DFS(*it);
            lowlink[x]=min( lowlink[*it], lowlink[x] );
            if( lowlink[*it] >= idx[x] )
                GetBCC( pr( x, *it ) );
        }
        else lowlink[x]=min( lowlink[x], idx[*it] );
    }

}
int main(int argc, char** argv)
{
    int N, 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';
    if( !nrc )
        return 0;
    for( x=1; x <= nrc; ++x )
    {
        copy( BCC[x].begin(), BCC[x].end(), ostream_iterator<int>( out, " " ) );
        out<<'\n';
    } 
    return (EXIT_SUCCESS);
}