Cod sursa(job #2922480)

Utilizator raresgherasaRares Gherasa raresgherasa Data 8 septembrie 2022 17:13:40
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NM = 1e5 + 5;

vector<int>g[NM];
bool used[NM];

void dfs (int nod){
  used[nod] = true;
  for (int u : g[nod]){
    if (used[u] == false){
      dfs(u);
    }
  }
}

int main(){
  int n, m; fin >> n >> m;
  while (m--){
    int x, y; fin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++){
    if (!used[i]){
      ans += 1;
      dfs(i);
    }
  }
  fout << ans;
}