Cod sursa(job #2211718)

Utilizator PetyAlexandru Peticaru Pety Data 11 iunie 2018 15:30:19
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int>v[100002];
int n, m, x, y, nr, viz[100002];

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

int main()
{
  fin >> n >> m;
  for (int i = 1; i <= m; i++) {
    fin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (viz[i] == 0) {
      DFS(i);
      nr++;
    }
  }
  fout << nr;
  return 0;
}