Cod sursa(job #1358133)

Utilizator fetti_danutzdezactivat fetti_danutz Data 24 februarie 2015 13:21:07
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#include <vector>
using namespace std;

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

#define DIM 100001

int n, m;
vector<bool> v;
vector<int> G[DIM];
int cont;

void DF(int x);

int main()
{
    fin >> n >> m;
    v.resize(n + 1);
    int x, y;
    for ( int i = 1; i <= m; ++i )
    {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for ( int i = 1; i <= n; ++i )
        if ( !v[i] )
        {
            cont++;
            DF(i);
        }
    fout << cont;
    fin.close();
    fout.close();
    return 0;
}

void DF(int x)
{
    v[x] = true;

    for ( const auto& e : G[x] )
        if ( !v[e] )
            DF(e);

};