Cod sursa(job #2869755)

Utilizator Stefan_BircaBirca Stefan Stefan_Birca Data 11 martie 2022 20:04:44
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, viz[100005];
vector <int> h[100005];

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

int main()
{

    int i, x, y, nrcc = 0, m;
    fin >> n >> m;
    while (m--)
    {
        fin >> x >> y;
        h[x].push_back(y);
        h[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (!viz[i])
        {
            nrcc++;
            DFS(i);
        }
    fout << nrcc << "\n";
    fin.close();
    fout.close();

    return 0;
}