Cod sursa(job #1194386)

Utilizator hasmasandragosHasmasan Dragos hasmasandragos Data 3 iunie 2014 18:58:10
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#include <vector>
#define Nmax 200005
using namespace std;

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

int j,i,n,m,x,y,nrcnx,viz[Nmax/2];
vector <int> a[Nmax];

void dfs (int x)
{ int h;
  for (h=0;h<a[x].size();h++)
   {
       if (viz[a[x][h]]==0)
       {viz[a[x][h]]=1;
        dfs(a[x][h]);
       }
   }
}

int main()
{
    f>>n>>m;
    for (i=1;i<=m;i++)
     {
         f>>x>>y;
         a[x].push_back(y);
         a[y].push_back(x);
     }
    for (j=1;j<=n;j++)
    {
        if (!viz[j])
         {
             viz[j]=1;
             dfs(j);
             nrcnx++;
         }
    }
    g<<nrcnx<<'\n';
    return 0;
}