Cod sursa(job #2372885)

Utilizator rebeca.d31Diaconu Rebeca-Mihaela rebeca.d31 Data 7 martie 2019 11:26:37
Problema Parcurgere DFS - componente conexe Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

#define nrmax 10000

vector <int> g[nrmax];

int v[10000];

void dfs(int x){
    v[x] = 1;
    int lim = g[x].size();
    for(int i = 0; i<lim; i++) {
        if(v[g[x][i]] == 0) dfs(g[x][i]);
    }
}

int main()
{

    int compConex = 0, m, n;
    ifstream f("dfs.in");
    ofstream p("dfs.out");
    f>>n>>m;
    for(int i=0; i<m; i++){
        int x, y;
        f>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
   }

   for(int i=1; i<=n; i++)
        if(v[i] == 0){
            dfs(i);
            compConex++;
        }

    p<<compConex;

   return 0;

}