Cod sursa(job #1482573)

Utilizator ataegasMr. HHHHHH ataegas Data 7 septembrie 2015 16:03:34
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100005
using namespace std;

vector <int> v[nmax];
bool seen[nmax];

void get_data(int &n){
    ifstream fin("dfs.in");
    int x, y, m;
    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){
    seen[x]= true;
    for(auto i: v[x])
        if(!seen[i])    dfs(i);
}
int main(){
    int n, components= 0;
    get_data(n);
    for(int i= 1; i<=n; i++)
        if(!seen[i])    dfs(i), components++;
    ofstream fout ("dfs.out");
    fout << components;
    return 0;
}