Cod sursa(job #1115699)

Utilizator robertstrecheStreche Robert robertstreche Data 21 februarie 2014 22:54:39
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");

int n,m,y,x,i,nr,b[100001];
int a[100001];
vector <int> v[100001];

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

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

     }

   for (i=1;i<=n;i++)
     if (!a[i])
      {
          nr++;
          dfs(i);
      }

     g<<nr;

   f.close();
   g.close();
}