Cod sursa(job #447356)

Utilizator alexandru92alexandru alexandru92 Data 28 aprilie 2010 15:31:10
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on April 28, 2010, 3:22 PM
 */
#include <vector>
#include <cstdlib>
#include <fstream>
#define Nmax 100011

/*
 * 
 */
using namespace std;
bool uz[Nmax];
vector< int > G[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);
}
int main(int argc, char** argv)
{
    int N, M, x, y;
    ifstream in( "dfs.in" );
    for( in>>N>>M; M; --M )
    {
        in>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for( y=0, x=1; x <= N; ++x )
        if( !uz[x] )
        {
            ++y;
            DFS(x);
        }
    ofstream out( "dfs.out" );
    out<<y<<'\n';
    return (EXIT_SUCCESS);
}