Cod sursa(job #2869136)

Utilizator Rares1707Suchea Rares-Andrei Rares1707 Data 11 martie 2022 12:46:39
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int nrNoduri, nrMuchii, nrComponenteConexe;

bool parcurs[100005];
vector < int > legaturi[100005];

void Citire()
{
    int x, y;
    fin >> nrNoduri >> nrMuchii;
    for (int i = 0; i < nrMuchii; i++)
    {
        fin >> x >> y;
        legaturi[x].push_back(y);
        legaturi[y].push_back(x);
    }
}

void DFS(int nodCurent)
{
    parcurs[nodCurent] = true;
    for (int i = 0; i < legaturi[nodCurent].size(); i++)
    {
        int nodUrmator = legaturi[nodCurent][i];
        if (parcurs[nodUrmator] == false)
        {
            DFS(nodUrmator);
        }
    }
}

int main()
{
    Citire();
    for (int i = 1; i <= nrNoduri; i++)
    {
        if (parcurs[i] == false)
        {
            nrComponenteConexe++;
            DFS(i);
        }
    }
    fout << nrComponenteConexe;
    return 0;
}