Cod sursa(job #2267089)

Utilizator urweakurweak urweak Data 23 octombrie 2018 11:16:54
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");

int insule = 0;
const int NLIM = 100005;
bool vizitat[NLIM];
int N, M; // Noduri, Muchii

vector < int > Muchii[NLIM];

void DFS(int Nod){
    vizitat[Nod] = true;
    for(unsigned i = 0; i < Muchii[Nod].size(); i++){
        int vecin = Muchii[Nod][i];
        if(!vizitat[vecin])
            DFS(vecin);
    }
}

void Citire(){
    fin >> N >> M;
    for(int i = 1; i<=M; i++){
        int x , y;
        fin >> x >> y;
        Muchii[x].push_back(y);
        Muchii[y].push_back(x);
    }

    for(int i = 1; i<=N; i++)
    {
        if(!vizitat[i]){
            insule+=1;
            DFS(i);
        }
    }
}

int main(){

    Citire();
     fout << insule <<'\n';
}