Cod sursa(job #3183629)

Utilizator picalexPicioroaga Alexandru picalex Data 12 decembrie 2023 16:48:25
Problema Parcurgere DFS - componente conexe Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>
#include <vector>
#define MAX 100001
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");

bool use[MAX];
vector<int> v[MAX];
void dfs(int nod)
{
    use[nod] = true;
    for(int i=0;i<v[nod].size();i++)
    {
        if (!use[v[nod][i]])
            dfs(v[nod][i]);
    }
}

int main()
{
    int n, m; fin >> n >> m;
    for(int i=0;i<m;i++)
    {
        int x, y; fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    int nrComponente = 0;
    for (int i = 0; i < n; i++)
        if (!use[i]) {
            dfs(i);
            nrComponente++;
        }
    fout << nrComponente;
    return 0;
}