Cod sursa(job #2798029)
| Utilizator | Data | 10 noiembrie 2021 20:34:19 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 15 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.69 kb |
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100001
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m;
vector<int> la[nmax];
bool viz[nmax];
void dfs(int nod)
{
viz[nod] = 1;
for(auto i:la[nod])
{
if(viz[i] == 0)
{
viz[i] = 1;
dfs(i);
}
}
}
int main()
{
int x, y, nr_comp = 0;
f >> n >> m ;
for(int i = 1; i <= m; i++)
{
f >> x >> y;
la[x].push_back(y);
}
for(int i = 1; i <= n; i++)
if(viz[i] == 0)
{
nr_comp++;
dfs(i);
}
g << nr_comp;
return 0;
}
