Cod sursa(job #2031994)

Utilizator AngelEclipseGarleanu Alexandru Stefan AngelEclipse Data 4 octombrie 2017 10:23:37
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int m, n, tempNod, tempElem, conex;

vector < int > graf[100000];

bool viz[100000];

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

void read() {

    f>>n>>m;

    for(int i = 0; i < m; i++){
        f>>tempNod>>tempElem;
        graf[tempNod].push_back(tempElem);
        graf[tempElem].push_back(tempNod);
    }

}

void DFS( int a ) {

    int c = 0;

        while(c < graf[a].size()){

            if(!viz[graf[a][c]]){

                viz[graf[a][c]] = 1;

                DFS(graf[a][c]);
            }

            c++;
        }


}

int main()
{


    read();

    for(int i = 1; i <= n; i++){
        if(!viz[i])
        {
            conex++;
            DFS(i);
        }

    }

    g<<conex;

    return 0;
}