Cod sursa(job #2355997)

Utilizator onaiculykculDavid Alin onaiculykcul Data 26 februarie 2019 13:51:29
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <vector>



using namespace std;
#define MAX_N 100001
vector <int> Muchii[MAX_N];
int conexe = 0;
int M,N;
bool viz[MAX_N];
ifstream fin("dfs.in");
ofstream fout("dfs.out");

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


void citire()
{
    fin>>N>>M;
    for(int i = 0 ; i < M; i++)
    {
        int coefx,coefy;
        fin>>coefx>>coefy;
        Muchii[coefx].push_back(coefy);
        Muchii[coefy].push_back(coefx);
    }
    for(int i = 1 ; i <= N ; i++)
    {
        if(!viz[i])
        {
            DFS(i);
           conexe++;
        }
    }
    fin.close();
}

int main()
{
    citire();
    fout<<conexe;
    fout.close();
    return 0;
}