Cod sursa(job #2785180)

Utilizator Tache_RoxanaTache Roxana Tache_Roxana Data 18 octombrie 2021 09:36:51
Problema Parcurgere DFS - componente conexe Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
using namespace std;

#define maxim 100002

int n, m, vizitate[maxim], nrComponente;
list<int> nevizitate;
vector<int> a[maxim];

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

void citire() {
    in >> n >> m;
    int x, y;
    for(int i = 1; i <= m; i++){
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
}

void dfs(int x){
    list<int>::iterator i;
    vizitate[x] = 1;
    for (auto i = nevizitate.begin(); i != nevizitate.end(); i++)
        if(x == *i){
            nevizitate.erase(i);
            break;
        }
    for(auto j: a[x])
        if(vizitate[j] == 0)
            dfs(j);
}

int main()
{
    citire();
    for(int i = 1; i <= n; i++)
        nevizitate.push_back(i);
    while(nevizitate.empty() == 0){
        nrComponente++;
        dfs(nevizitate.front());
    }
    out << nrComponente;
    return 0;
}