Cod sursa(job #2886910)

Utilizator Tudor_EnacheEnache Tudor Tudor_Enache Data 8 aprilie 2022 16:33:00
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include<fstream>
#include <vector>
using namespace std;

ifstream cin ("dfs.in");
ofstream cout("dfs.out");
int n,m, insule = 0;
const int NLIM = 100005;

bool vizitat[NLIM];
vector <int> Muchii[NLIM];

void dfs(int k){
    vizitat[k] = 1;
    for (unsigned int i = 0; i <Muchii[k].size();++i){
        int vecin = Muchii[k][i];
        if (!vizitat[vecin]){
            dfs(vecin);
        }
    }
}
void citire(){
    cin >> n >> m;
    for (int i = 1; i <=m;++i)
    {
        int x,y;
        cin >> x >> y;
        Muchii[x].push_back(y);
        Muchii[y].push_back(x);
    }
    for (int i = 1; i <=n;++i){
        if (!vizitat[i]){
            insule++;
            dfs(i);
        }
    }
}
int main(){
    citire();
    cout << insule << "\n";
}