Pagini recente » Utilizatori inregistrati la preONI 2008, Runda 1, Clasele 5-8 | Cod sursa (job #516452) | Cod sursa (job #1978695) | Utilizatori inregistrati la Infoarena Monthly 2012 - Runda 6 | Cod sursa (job #2104338)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int NLIM = 100005;
int N, M;
bool vizitat[NLIM];
vector < int > Muchii[NLIM];
int insule = 0;
void DFS(int Nod)
{
vizitat[Nod] = true;
for(unsigned int 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);
}
}
fout << insule << "\n";
}
int main() {
Citire();
return 0;
}