Cod sursa(job #2900112)
| Utilizator | Data | 10 mai 2022 11:49:26 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-32 | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, viz[100005], sol;
vector <int> a[100005];
void Dfs(int x)
{
viz[x] = 1;
for (int i = 0; i < a[x].size(); i++)
if (viz[a[x][i]] == 0)
Dfs(a[x][i]);
}
int main()
{
int i, j, x, y;
fin >> n >> m;
for (i = 1; i <= m; i++)
{
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for (i = 1; i <= n; i++)
if (viz[i] == 0)
{
Dfs(i);
sol++;
}
fout << sol << "\n";
return 0;
}