Cod sursa(job #2032000)

Utilizator AngelEclipseGarleanu Alexandru Stefan AngelEclipse Data 4 octombrie 2017 10:28:15
Problema Parcurgere DFS - componente conexe Scor 100
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[100001];

bool viz[100001];

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;
}