Cod sursa(job #2955215)

Utilizator LukyenDracea Lucian Lukyen Data 16 decembrie 2022 16:17:50
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
using namespace std;
const int vec_len = 200001;

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

int n, m;
vector<vector<int>> graph(vec_len);

bitset<vec_len> vis;
void dfs(int node)
{
  if (vis[node])
    return;

  vis[node] = true;

  for (int next : graph[node])
    dfs(next);
}

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

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

  fout << res;
  return 0;
}