Cod sursa(job #1261143)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 11 noiembrie 2014 23:27:10
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
#define nmax 200005
#define pb push_back

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

int n, m, i, x, y, j, elemente;
bool vizitat[nmax];

vector <int> v[nmax];

void dfs (int x){
    for(int j=0; j<v[x].size(); j++)
        if(!vizitat[v[x][j]]){
            vizitat[v[x][j]]=true;
            dfs(v[x][j]);
        }
}

int main()
{
    fin >> n >> m;
    for(i=1; i<=m; i++){
        fin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }

    for(i=1; i<=n; i++){
        if(!vizitat[i]){
            vizitat[i]=true;
            elemente++;
            dfs(i);
        }
    }
    fout << elemente;
    return 0;
}