Mai intai trebuie sa te autentifici.

Cod sursa(job #1548549)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 11 decembrie 2015 00:32:06
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100005
using namespace std;

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

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

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

int main(){
    int n, m;
    bool seen[nmax];
    vector <int> v[nmax];
    get_data (n, m, v);
    output_data(n, v, seen);
    return 0;
}