Pagini recente » Cod sursa (job #38780) | Cod sursa (job #3130289) | Cod sursa (job #26271) | Cod sursa (job #3222097) | Cod sursa (job #2606948)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
int n, m;
int main()
{
fin >> n >> m;
vector < vector < int > > graph(n + 1);
bool viz[n + 1] = {0};
for(int i = 1 ; i <= m ; i++)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
std::function<void(int)> dfs = [&](int nod)
{
viz[nod] = 1;
for (auto& it : graph[nod])
if (!viz[it])
{
///std::cout << it << " ";
dfs(it);
}
};
int ans = 0;
for(int i = 1 ; i <= n ; i++)
if(!viz[i])
{
ans++;
dfs(i);
}
fout << ans << "\n";
fin.close();
fout.close();
return 0;
}