Cod sursa(job #2678089)

Utilizator codruta.miron08Miron Ioana Codruta codruta.miron08 Data 28 noiembrie 2020 10:15:17
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
#include <iostream>

using namespace std;

bool viz[100005];
vector<int> nodes[100005];
int n, m;
 ifstream fin("dfs.in");
    ofstream fout("dfs.out");
void dfs(int node){
    viz[node] = true;
    for (auto &it : nodes[node]) {
        if(!viz[it]){
            dfs(it);
        }
    }
}

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