Cod sursa(job #460426)

Utilizator BitOneSAlexandru BitOne Data 2 iunie 2010 16:18:33
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 100111

/*
 *
 */
using namespace std;
int indx, nrc;
int idx[Nmax], lowlink[Nmax];
stack< int > S;
vector< int > G[Nmax], SCC[Nmax];
inline void DFS( int x )
{
    S.push(x);
    idx[x]=lowlink[x]=++indx;
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( 0 == idx[*it] )
        {
            DFS( *it );
            lowlink[x]=min( lowlink[x], lowlink[*it] );
        }
        else lowlink[x]=min( lowlink[x], idx[*it] );
    if( idx[x] == lowlink[x] )
    {
        int y;
        ++nrc;
        do
        {
            y=S.top(); S.pop();
            SCC[nrc].push_back(y);
        }while( y != x );
    }
}
int main( void )
{
    int N, M, x, y;
    ifstream in( "ctc.in" );
    for( in>>N>>M; M; --M )
    {
        in>>x>>y;
        G[x].push_back(y);
    }
    for( x=1; x <= N; ++x )
        if( 0 == idx[x] )
            DFS(x);
    ofstream out( "ctc.out" );
    out<<nrc<<'\n';
    for( x=1; x <= nrc; ++x )
    {
        copy( SCC[x].begin(), SCC[x].end(), ostream_iterator< int >( out, " " ) );
        out<<'\n';
    }
    return EXIT_SUCCESS;
}