Cod sursa(job #2517016)

Utilizator TocuAndreiTocu Andrei TocuAndrei Data 2 ianuarie 2020 19:18:22
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");

const int nMax = 100002;
unsigned int conexe = 0, n, m;
vector <int> Noduri[nMax];
bool vizit[nMax];

void DFS(unsigned int pozitie) {
    vizit[pozitie] = true;
    for(unsigned int j = 0; j < Noduri[pozitie].size(); j++) {
        int next = Noduri[pozitie][j];
        if(!vizit[next]) {
            DFS(next);
        }
    }
}

void citire() {
    fin >> n >> m;
    for(unsigned int i = 1; i <= m; i ++) {
        int x, y;
        fin >> x >> y;
        Noduri[x].push_back(y);
        Noduri[y].push_back(x);
    }
    for(unsigned int i = 1; i <= n; i++) {
        if(!vizit[i]) {
            conexe ++;
            DFS(i);
        }
    }
}

int main()
{
    citire();
    fout << conexe;
    return 0;
}