Cod sursa(job #2315859)

Utilizator stefii_predaStefania Preda stefii_preda Data 10 ianuarie 2019 18:11:46
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");

const int N = 100002;

vector <int> G[N];
int n, m;
bool vis[N];

void dfs (int node){
    vis[node] = true;
    for(int i = 0; i < G[node].size(); i++){
        if(vis[G[node][i]] == false)
            dfs(G[node][i]);
    }

}

int main()
{

    in >> n >> m;
    int x, y, i;
    for(i = 1; i <= m; i++){
        in >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    int nrcomp = 0;
    for(i = 1; i <= n; i++){
        if(vis[i] == false){
            nrcomp ++;
            dfs(i);
        }
    }
    out << nrcomp;

    return 0;
}