Cod sursa(job #455330)

Utilizator alexandru92alexandru alexandru92 Data 13 mai 2010 15:38:59
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on May 12, 2010, 6:17 PM
 */
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 100011

/*
 *
 */
using namespace std;
int nrct;
bool uz[Nmax];
vector< int > dfs;
vector< int > G[Nmax], Gt[Nmax], ctc[Nmax];
inline void DFS( int x )
{
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    uz[x]=true;
    for( ; it < iend; ++it )
        if( !uz[*it] )
            DFS(*it);
    dfs.push_back(x);
}
inline void DFST( int x )
{
    vector< int >::const_iterator it=Gt[x].begin(), iend=Gt[x].end();
    uz[x]=false;
    for( ; it < iend; ++it )
        if( uz[*it] )
            DFST(*it);
    ctc[nrct].push_back(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);
        Gt[y].push_back(x);
    }
    for( x=1; x <= N; ++x )
        if( !uz[x] )
            DFS(x);
    for( x=N-1; x >= 0; --x )
        if( uz[dfs[x]] )
        {
            ++nrct;
            DFST( dfs[x] );
        }
    ofstream out( "ctc.out" );
    out<<nrct<<'\n';
    for( x=1; x <= nrct; ++x )
    {
        copy( ctc[x].begin(), ctc[x].end(), ostream_iterator<int>( out, " " ) );
        out<<'\n';
    }
    return EXIT_SUCCESS;
}