Pagini recente » Monitorul de evaluare | Cod sursa (job #2842731) | Cod sursa (job #389125) | Cod sursa (job #3313321) | Cod sursa (job #3314245)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
vector<int> a[100005];
int viz[100005];
// void bfs(int nod)
// {
// queue<int> q;
// q.push(nod);
// cost[nod] = 0;
// while (!q.empty())
// {
// int actNod = q.front();
// q.pop();
// for (auto next : a[actNod])
// if (cost[next] > cost[actNod] + 1)
// {
// cost[next] = cost[actNod] + 1;
// q.push(next);
// }
// }
// }
void dfs(int nod)
{
viz[nod] = 1;
for (auto w : a[nod])
{
if (!viz[w])
dfs(w);
}
}
int main()
{
int n, m, s;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (viz[i] == 0)
{
dfs(i);
cnt++;
}
}
fout << cnt << '\n';
return 0;
}