Cod sursa(job #2889993)

Utilizator carmenacatrineiCarmen-Lorena Acatrinei carmenacatrinei Data 14 aprilie 2022 04:28:40
Problema Parcurgere DFS - componente conexe Scor 45
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

class N_AUD{

    vector < bool > vis;

    void dfs(int node, vector < vector < int > > muchii) {

        vis[node] = true;

        for (int index = 0; index < muchii[node].size(); ++index) {
            int nextNode = muchii[node][index];

            if (!vis[nextNode]) {
                dfs(nextNode, muchii);
            }
        }

    }

public:

    int numarComponenteConexe(vector < vector < int > > muchii) {

        int counter = 0;
        int n = muchii.size() - 1;

        vis.resize(n + 1, false);

        for (int node = 1; node <= n; ++node)
            if (!vis[node]) {
                ++counter;
                dfs(node, muchii);
            }
        return counter;

    }

};

int main()
{
    N_AUD graph;
    vector < vector < int > > muchii;
    int n, m, x, y;

    in >> n >> m;

    muchii.resize(n + 1);

    for (int index = 1; index <= m; ++index) {
        in >> x >> y;

        muchii[x].push_back(y);
        muchii[y].push_back(x);
    }

    out << graph.numarComponenteConexe(muchii);
    return 0;
}