Cod sursa(job #2543063)

Utilizator Horia14Horia Banciu Horia14 Data 10 februarie 2020 20:08:42
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include<fstream>
#include<iostream>
#include<vector>
#define MAX_VERTICES 100000
using namespace std;

vector<int>g[MAX_VERTICES + 1];
bool used[MAX_VERTICES + 1];
int n, m, nrc;

void readGraph() {
    int x, y;
    ifstream fin("dfs.in");
    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
        //cout << x << " " << y << "\n";
    }
    fin.close();
}

void DFS(int node) {
    used[node] = 1;
    for(auto i : g[node]) {
        if(!used[i])
            DFS(i);
    }
}

void DFS_master() {
    for(int i = 1; i <= n; i++) {
        if(!used[i]) {
            nrc++;
            DFS(i);
        }
    }
}

void printNrc() {
    ofstream fout("dfs.out");
    fout << nrc << "\n";
    fout.close();
}

int main() {
    readGraph();
    DFS_master();
    printNrc();
    return 0;
}