Cod sursa(job #714223)

Utilizator morlockRadu Tatomir morlock Data 15 martie 2012 16:15:45
Problema Parcurgere DFS - componente conexe Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#define nmax 10005
using namespace std;

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

vector<int> v[nmax];
vector<int> sol;

int n, m, comp=0, viz[nmax];

void citeste()
{ int x, y;

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

void dfs(int nod)
{
    viz[nod] = 1;
    for (unsigned i = 0; i < v[nod].size(); ++i )
     {
         if ( viz[v[nod][i]] == 0 )
          dfs( v[nod][i] );
     }

}


int main()
{
    citeste();

    for (int i=1; i<=n; ++i)
     if ( !viz[i] )
      {
          ++comp;
          dfs(i);
      }

    out<<comp;

return 0;
}