Cod sursa(job #2387520)

Utilizator ICHBogdanIordache Bogdan-Mihai ICHBogdan Data 24 martie 2019 20:02:51
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

vector <int> muchii[10];
bool viz[10];

void dfs(int s)
{
    viz[s] = true;
    for(int i = 0; i < muchii[s].size(); ++i)
    {
        if(viz[muchii[s][i]] == false)
            dfs(muchii[s][i]);
    }
}

void initialize()
{
    for(int i = 0; i < 10; ++i)
        viz[i] = false;
}

int main()
{

    ifstream fin("dfs.in");
    ofstream fout("dfs.out");
    int noduri, numarMuchii, x, y, componenteConexe = 0;
    fin>>noduri>>numarMuchii;
    for(int i = 0; i < numarMuchii; ++i)
    {
        fin >> x >> y;
        muchii[x].push_back(y);
        muchii[y].push_back(x);
    }
    fin.close();
    initialize();

    for(int i = 1; i <= noduri; ++i)
    {
        if(viz[i] == false)
        {
            dfs(i);
            componenteConexe++;
        }
    }
    fout<<componenteConexe;
    fout.close();
    return 0;
}