Cod sursa(job #3154975)

Utilizator PescarusTanislav Luca Andrei Pescarus Data 6 octombrie 2023 23:11:36
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int nmax = 100005;

vector<int> v[nmax];
bool viz[nmax];
int n, m;
void dfs(int node){
  viz[node] = true;
  for(int vec : v[node]){
    if(!viz[vec]){
      dfs(vec);
    }
  }
}
int main(){
  f >> n >> m;
  for(int i = 1; i <= m; i++){
    int x, y;
    f >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int sol = 0;
  for(int i = 1; i <= n; i++){
    if(!viz[i]){
      sol++;
      dfs(i);
    }
  }
  g << sol << '\n';
}