Cod sursa(job #2196386)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 19 aprilie 2018 10:13:07
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

vector<int> v[100010];
int vaz[100010];
int n,m,x,y,sol=0;
void dfs( int nod )
{
    vaz[nod] = 1;
    for ( int i = 0 ; i < v[nod].size() ; i++ )
        if( vaz[v[nod][i]] == 0)
        dfs(v[nod][i]);
}
int main()
{
   f >> n >> m;
   for(int  i = 1 ; i <= m ; i++ )
   {
       f >> x >> y ;
       v[x].push_back(y);
       v[y].push_back(x);
   }
   for(int  i = 1 ; i <= n ; i++ )
   if( vaz[i] == 0 )
   {
       sol++;
       dfs(i);
   }
   g << sol;
    return 0;
}