Cod sursa(job #1453713)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 24 iunie 2015 13:05:06
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100005
using namespace std;

bool seen[nmax];

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

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

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