Pagini recente » Cod sursa (job #3140080) | Cod sursa (job #1759837) | Cod sursa (job #1465450) | Cod sursa (job #96901) | Cod sursa (job #3241154)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector<vector<int>> graph(100001);
vector<bool> visited(100001, false);
void dfs(int node)
{
visited[node] = true;
for (auto neighbor : graph[node])
{
if (!visited[neighbor])
dfs(neighbor);
}
}
int main()
{
int n, m, cnt = 0;
fin >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (!visited[i])
{
cnt++;
dfs(i);
}
fout << cnt;
return 0;
}