Cod sursa(job #3368050)

Utilizator RaducuSebasRaducu Sebastian RaducuSebas Data 13 octombrie 2026 10:22:27
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fi("dfs.in");
ofstream fo("dfs.out");

int n, m;
vector<int> adj[100005];
bool viz[100005];

void dfs(int nod) {
  viz[nod] = true;
  for (int vecin : adj[nod])
    if (!viz[vecin])
      dfs(vecin);
}

int main() {
  fi >> n >> m;
  int x, y;
  for (int i = 1; i <= m; i++) {
    fi >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  int cc = 0;
  for (int i = 1; i <= n; i++)
    if (!viz[i]) {
      cc++;
      dfs(i);
    }

  fo << cc << "\n";
  return 0;
}