Cod sursa(job #1261115)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 11 noiembrie 2014 23:00:07
Problema Parcurgere DFS - componente conexe Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
#define nmax 100005
#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);
    }
    cout << v[1][1];

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