Pagini recente » Cod sursa (job #2984977) | Cod sursa (job #2981665) | Cod sursa (job #2422713) | Cod sursa (job #2982947) | Cod sursa (job #2953918)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int NMAX = 100000;
vector <int> g[NMAX + 1];
bool viz[NMAX + 1];
int cnt = 0;
void dfs(int x)
{
viz[x] = true;
///out << "nodul " << x << " poate merge in ";
for (int y: g[x])
{
if (!viz[y])
{
///out << y;
///out << "\n ";
viz[y] = true;
dfs(y);
}
}
///out << ";";
}
int main()
{
int n, m;
///citire
in >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
in >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
///dfs;
for (int i = 1; i <= n; i++)
{
if (!viz[i])
{
cnt++;
dfs(i);
}
}
///afisare
out << cnt;
return 0;
}