Cod sursa(job #2674879)

Utilizator DooMeDCristian Alexutan DooMeD Data 20 noiembrie 2020 16:59:24
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

vector <int> G[100005];
vector <int> ::iterator nod_vecin;
bool viz[100005];
int n,m,x,y,cnt;

void DFS(int nod_curent) {
  viz[nod_curent]=true;
  for(nod_vecin=G[nod_curent].begin(); nod_vecin!=G[nod_curent].end(); ++nod_vecin)
    if(!viz[*nod_vecin])
      DFS(*nod_vecin);
}

int main() {
  f >> n >> m;
  for(int i=1; i<=m; ++i) {
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for(int i=1; i<=n; i++)
    if(!viz[i]) {
      cnt++;
      DFS(i);
    }
  g << cnt;
  return 0;
}