Cod sursa(job #2568160)
| Utilizator | Data | 3 martie 2020 21:11:07 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
#include <fstream>
#include <vector>
using namespace std;
int n, m, v[100005];
vector < int > L[100005];
void DFS(int vertex)
{
v[vertex] = 1;
for(auto w : L[vertex])
if(!v[w])
DFS(w);
}
int main()
{
int x, y, nrc = 0;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
}
fin.close();
for(int i = 1; i <= n; i++)
if(!v[i])
{
nrc++;
DFS(i);
}
fout << nrc << "\n";
fout.close();
return 0;
}
