Cod sursa(job #2713337)

Utilizator AndreiMarcuAndrei Marcu AndreiMarcu Data 27 februarie 2021 18:47:54
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int NLIM = 100005;
vector <int> muchii[NLIM];
bool check[NLIM];

void dfs(int nod){
    check[nod] = 1;
    for(unsigned int i=0; i<muchii[nod].size(); i++){
        int next = muchii[nod][i];
        if(check[next]==0)
            dfs(next);
    }
}

int main()
{
    int n, m, insule = 0;

    in >> n >> m;

    for(int i=1; i<=m; i++){
        int x, y;
        in >> x >> y;
        muchii[x].push_back(y);
        muchii[y].push_back(x);
    }
    for(int i=1; i<=n; i++){
        if(check[i]==0){
    //        out << i << " ";
            insule++;
            dfs(i);
        }
    }

    out << insule;
    return 0;
}