Cod sursa(job #436157)

Utilizator alexandru92alexandru alexandru92 Data 8 aprilie 2010 08:56:20
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
/* 
 * File:   main.cpp
 * Author: VirtualDemon
 *
 * Created on April 8, 2010, 8:50 AM
 */
#include <vector>
#include <cstdlib>
#include <fstream>

/*
 * 
 */
using namespace std;
vector< bool > uz;
vector< vector< int > > G;
inline void DFS( int x )
{
    uz[x]=true;
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( !uz[*it] )
            DFS( *it );
}
int main(int argc, char** argv)
{
    int N, M, x, y;
    ifstream in( "dfs.in" );
    in>>N>>M;
    G.resize(N);
    uz.resize(N);
    for( ; M; --M )
    {
        in>>x>>y;
        --x, --y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for( x=y=0; x < N; ++x )
        if( !uz[x] )
        {
            ++y;
            DFS(x);
        }
    ofstream out( "dfs.out" );
    out<<y<<'\n';
    return EXIT_SUCCESS;
}