Cod sursa(job #1973712)

Utilizator MihaelaCismaruMihaela Cismaru MihaelaCismaru Data 25 aprilie 2017 19:09:41
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream in ("dfs.in" );
ofstream out("dfs.out");
vector<int>ha[200000];
int v[200001],hz[200001];
int n,m,i,st,dr,a,b,j,sol;
void dfs( int x ){
    int i;
    hz[x] = 1;
    for( i = 0; i < ha[x].size(); i ++ ){
        if(hz[ ha[x][i] ] == 0){
            dfs( ha[x][i] );
        }
    }


}
int main(){
    in >> n;
    in >> m;
    for( i = 1; i <= m; i ++ ){
        in >> a >> b;
        ha[a].push_back(b);
        ha[b].push_back(a);
    }
    for( i = 1; i <= n; i ++ ){
        if( hz[i] == 0 ){
            dfs(i);
            sol ++;
        }
    }
    out<<sol;

    return 0;
}