Pagini recente » Cod sursa (job #1670078) | Cod sursa (job #3272520) | Cod sursa (job #1945512) | Cod sursa (job #293198) | Cod sursa (job #2668242)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector<int> muchii[100010];
map<int, bool> vizitat;
void dfs(int nodCurent) {
vizitat[nodCurent] = true;
for (int vecin : muchii[nodCurent]) {
if (!vizitat[vecin]) {
dfs(vecin);
}
}
}
int main() {
int nrCompConexe = 0;
int n, m, x, y;
fin >> n >> m;
for (int i = 1; i <=m; i++) {
fin >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
for (int nod = 1;nod <= n;nod++) {
if (!vizitat[nod]) {
nrCompConexe++;
dfs(nod);
}
}
fout << nrCompConexe;
}