Cod sursa(job #2582852)
| Utilizator | Data | 17 martie 2020 13:48:43 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m, x, y;
bool u[100001];
vector < int > v[100001];
void dfs(int nod)
{
u[nod] = 1;
for (int i=0; i<v[nod].size(); i++)
if (!u[v[nod][i]])
dfs(v[nod][i]);
}
int main()
{
f >> n >> m;
for (int i=1; i<=m; i++)
{
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
int nrComp = 0;
for (int i=1; i<=n; i++)
if (!u[i])
{
nrComp++;
dfs(i);
}
g << nrComp;
return 0;
}
