Cod sursa(job #1463256)

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

bool seen[nmax];
vector<int> v[nmax];
int n, m, components, x, y;

ifstream fin ("dfs.in");
ofstream fout ("dfs.out");

void dfs(int x){
    seen[x]= true;
    for(auto i: v[x])
        if(!seen[i])    dfs(i);
}

int main(){
    fin >> n >> m;
    for(int i=1; i<=m; i++){
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1; i<=n; i++)
        if(!seen[i])    dfs(i), components++;
    fout << components;
}