Pagini recente » Cod sursa (job #1457586) | Cod sursa (job #7976) | Cod sursa (job #2861744) | Cod sursa (job #1599894) | Cod sursa (job #2740646)
#include <fstream>
#include <map>
#include <vector>
using namespace std;
ifstream cin("dfs.in");
ofstream cout("dfs.out");
const int nmax = 1e5;
int N, M, componente_conexe, x, y;
vector <int> muchii[nmax + 1];
map <int, bool> vizitat;
void DFS(int nod){
vizitat[nod] = true;
for(int i = 0; i < muchii[nod].size(); ++i){
if(!vizitat[muchii[nod][i]]){
DFS(muchii[nod][i]);
}
}
}
int main(){
cin >> N >> M;
while(M--){
cin >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
for(int i = 1; i <= N; ++i){
if(!vizitat[i]){
componente_conexe++;
DFS(i);
}
}
cout << componente_conexe;
return 0;
}