Pagini recente » Cod sursa (job #1163505) | Cod sursa (job #1107221) | Cod sursa (job #721227) | Cod sursa (job #354357) | Cod sursa (job #2517016)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int nMax = 100002;
unsigned int conexe = 0, n, m;
vector <int> Noduri[nMax];
bool vizit[nMax];
void DFS(unsigned int pozitie) {
vizit[pozitie] = true;
for(unsigned int j = 0; j < Noduri[pozitie].size(); j++) {
int next = Noduri[pozitie][j];
if(!vizit[next]) {
DFS(next);
}
}
}
void citire() {
fin >> n >> m;
for(unsigned int i = 1; i <= m; i ++) {
int x, y;
fin >> x >> y;
Noduri[x].push_back(y);
Noduri[y].push_back(x);
}
for(unsigned int i = 1; i <= n; i++) {
if(!vizit[i]) {
conexe ++;
DFS(i);
}
}
}
int main()
{
citire();
fout << conexe;
return 0;
}