Cod sursa(job #2667716)

Utilizator sebastian.barbarasaSebastian Barbarasa sebastian.barbarasa Data 3 noiembrie 2020 19:28:47
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#include <vector>
#define NMAX 100004
using namespace std;

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

vector <int> v[NMAX];
bool ok[NMAX];
int n, m, nrsol;

void bfs(int poz);

int main()
{
 int i, x, y;
 fin>>n>>m;
 for (i=1; i<=m; i++)
     {
      fin>>x>>y;
      v[x].push_back(y);
     }
 for (i=1; i<=n; i++)
      if (!ok[i])
          bfs(i),nrsol++;
 fout<<nrsol<<'\n';
 fin.close();
 fout.close();
 return 0;
}

void bfs(int poz)
{
 if (!ok[poz])
    {
     ok[poz]=1;
     while (!v[poz].empty())
           {
            bfs(v[poz].back());
            v[poz].pop_back();
           }
    }
}