Cod sursa(job #1588438)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 3 februarie 2016 08:20:56
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <vector>
#include <fstream>
#include <iostream>
#define nmax 100005
using namespace std;

void read_data (int &n, int &m, vector <int> v[nmax]){
    ifstream fin ("dfs.in");
    fin >> n >> m;
    for (int i = 1; i <= m; i++){
        int x, y;
        fin >> x >> y;
        v[x].push_back (y);
        v[y].push_back (x);
    }
}

void dfs (int current, vector <int> v[nmax], bool seen[nmax]){
    seen[current] = true;
    for (auto vecin : v[current])
        if (!seen[vecin])
            dfs (vecin, v, seen);
}

void print_answer (vector <int> v[nmax], bool seen[nmax], int n){
    ofstream fout ("dfs.out");
    int components = 0;
    for (int i = 1; i <= n; i++)
        if (!seen[i])
            dfs (i, v, seen), components ++;
    fout << components;
}

int main(){
    vector <int> v[nmax];
    bool seen[nmax];
    for (auto &x : seen)   x = false;
    int n, m;
    read_data (n, m, v);
    print_answer (v, seen, n);
    return 0;
}