Cod sursa(job #2922378)
| Utilizator | Data | 8 septembrie 2022 09:25:11 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector <int> a[100005];
int n, m;
bitset <100005> viz;
void Dfs(int x)
{
viz[x] = 1;
for (auto w : a[x])
if (viz[w] == 0)
Dfs(w);
}
int main()
{
int i, j, sol = 0;
fin >> n >> m;
while (m--)
{
fin >> i >> j;
a[i].push_back(j);
a[j].push_back(i);
}
for (i = 1; i <= n; i++)
if (viz[i] == 0)
{
Dfs(i);
sol++;
}
fout << sol << "\n";
return 0;
}
