Cod sursa(job #2678190)

Utilizator OffuruAndrei Rozmarin Offuru Data 28 noiembrie 2020 11:02:54
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
///parcurgere dfs
#include <bits/stdc++.h>

using namespace std;

bool vizitate[100005];
vector<int> nodes[100005];
int n, m;

void read()
{
    ifstream f("dfs.in");
    ofstream g("dfs.out");
    f>>n>>m;
    for (int i = 0; i < m; ++i) {
        int x, y;
        f>>x>>y;
        nodes[x].push_back(y);
        nodes[y].push_back(x);
    }
}

void dfs(int node){
    vizitate[node] = true;
    for (auto it : nodes[node]) {
        if(!vizitate[it]){
            dfs(it);
        }
    }
}

int main() {
    read();
    int nrComp = 0;
    for (int i = 1; i <= n; ++i) {
        if(!vizitate[i]){
            nrComp++;
            dfs(i);
            cout<<endl;
        }
    }
    g<<nrComp;
    return 0;
}