Pagini recente » preONI 2008 - Clasament Runda 4, Clasa a 10-a | Cod sursa (job #1124081) | Cod sursa (job #1121236) | Cod sursa (job #1664499) | Cod sursa (job #2104330)
#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;
/*
* 1: 2 4
* 2: 1
* 3: 5
* 4: 1
* 5: 3
* 6: -
*/
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 <= N; 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";
return 0;
}