Pagini recente » Cod sursa (job #1650740) | Cod sursa (job #2909966) | AndrewTheGreat | Cod sursa (job #282352) | Cod sursa (job #2267089)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int insule = 0;
const int NLIM = 100005;
bool vizitat[NLIM];
int N, M; // Noduri, Muchii
vector < int > Muchii[NLIM];
void DFS(int Nod){
vizitat[Nod] = true;
for(unsigned i = 0; i < Muchii[Nod].size(); i++){
int vecin = Muchii[Nod][i];
if(!vizitat[vecin])
DFS(vecin);
}
}
void Citire(){
fin >> N >> M;
for(int i = 1; i<=M; i++){
int x , y;
fin >> x >> y;
Muchii[x].push_back(y);
Muchii[y].push_back(x);
}
for(int i = 1; i<=N; i++)
{
if(!vizitat[i]){
insule+=1;
DFS(i);
}
}
}
int main(){
Citire();
fout << insule <<'\n';
}