Cod sursa(job #2398674)

Utilizator isa_tudor_andreiAndrei Tudor isa_tudor_andrei Data 5 aprilie 2019 20:31:18
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>
#include <vector>
#define NMAX 100000

using namespace std;

bool viz[NMAX + 1];
vector<int> g[NMAX];

void dfs( int x ) {
  viz[x] = 1;
  for( auto it : g[x] )
    if( viz[it] == 0 )
      dfs(it);
}

int main() {
    ifstream fin("dfs.in");
    ofstream fout("dfs.out");
    int n, i, m, a, b;
    fin>>n>>m;
    for( i = 1; i <= m; i ++ ) {
      fin>>a>>b;
      g[a].push_back(b);
      g[b].push_back(a);
    }
    int componente = 0;
    for( i = 1; i <= n; i ++ ) {
      if( viz[i] == 0 )
        dfs(i), componente ++;
    }
    fout<<componente;
    return 0;
}