Cod sursa(job #459954)

Utilizator BitOneSAlexandru BitOne Data 31 mai 2010 18:41:25
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#define Nmax 100111

/*
 *
 */
using namespace std;
bool was[Nmax];
vector< int > G[Nmax];
inline void DFS( int x )
{
    was[x]=true;
    if( G[x].empty() )
        return;
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( false == was[*it] )
            DFS(*it);
}
int main( void )
{
    int N, M, x, y, nr=0;
    ifstream in( "dfs.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( false == was[x] )
        {
            ++nr;
            DFS( x );
        }
    ofstream out( "dfs.out" );
    out<<nr<<'\n';
    return EXIT_SUCCESS;
}