Cod sursa(job #1819471)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 30 noiembrie 2016 17:00:42
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
# include <fstream>
# include <vector>

using namespace std;

# define MAX_N 100000
vector<int> v[MAX_N];
bool check[1 + MAX_N];

void dfs( const int &n )
{
    if ( !check[n] ) {
        check[n] = true;

        for ( int i : v[n] )
            dfs( i );
    }
}

int main()
{
    ifstream fin( "dfs.in" );
    ofstream fout( "dfs.out" );

    ios::sync_with_stdio( false );

    int n, m, i, a, b, r;

    fin >> n >> m;

    for ( i = 0; i < m; i ++ ) {
        fin >> a >> b;

        v[a].push_back( b );
        v[b].push_back( a );
    }

    r = 0;
    for ( i = 1; i <= n; i ++ )
        if ( !check[i] ) {
            r ++;
            dfs( i );
        }

    fout << r;

    fin.close();
    fout.close();

    return 0;
}