Cod sursa(job #2869687)

Utilizator Sebi_MafteiMaftei Sebastioan Sebi_Maftei Data 11 martie 2022 19:16:07
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m;
vector<int> h[100003];
bitset<100003> viz;

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

void DFS(int k)
{
    viz[k] = 1;
    for (int w : h[k])
        if (viz[w] == 0)
            DFS(w);
}

int main()
{
    int cnt = 0;
    Citire();
    for (int i = 1; i <= n; i++)
        if (viz[i] == 0)
        {
            cnt++;
            DFS(i);
        }
    fout << cnt;
    return 0;
}