Cod sursa(job #1192374)

Utilizator PaueyPaula Nicoleta Gradu Pauey Data 28 mai 2014 21:37:25
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <fstream>
#include <vector>
using namespace std;

bool viz[100001];
vector<int> vec[100001];

int n, m, a, b, nr;

void DFS(int G) {
   viz[G] = 1;
   for(int i = 0; i < vec[G].size(); ++i)
      if(!viz[vec[G][i]])
         DFS(vec[G][i]);
}

int main()
{
    ifstream in("dfs.in");
    ofstream out("dfs.out");
    in >> n >> m;
    for(int i = 1; i <= m; ++i) {
      in >> a >> b;
      vec[a].push_back(b);
      vec[b].push_back(a);
    }
    for(int i = 1; i <= n; ++i){
      if(!viz[i]) {
         ++nr;
         DFS(i);
      }
    }
    out << nr;
    return 0;
}