Cod sursa(job #1463259)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 20 iulie 2015 17:24:32
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <vector>
#include <fstream>
#define nmax 100005
using namespace std;

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

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

int main(){
    ofstream fout ("dfs.out");
    int n, components= 0;
    bool seen[nmax];
    for (auto &x: seen) x= false;
    vector<int> v[nmax];
    ios_base::sync_with_stdio(false);
    get_data(n, v);
    for(int i=1; i<=n; i++)
        if(!seen[i])    components++, dfs(i, seen, v);
    fout << components << " ";
    return 0;
}