Pagini recente » Istoria paginii runda/rar2/clasament | tema | Statistici free arena (free_arena) | tema | Cod sursa (job #2174548)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,insule;
vector <int> muchii[100002];
bool v[100002];
void citire()
{
int x,y;
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>x>>y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
}
void DFS(int nod)
{
v[nod]=true;
for(unsigned int i=0;i<muchii[nod].size();i++)
{
int vecin=muchii[nod][i];
if(v[vecin]==false)
DFS(vecin);
}
}
void rezolvare()
{
for(int i=1;i<=n;i++)
if(v[i]==0)
{
insule++;
DFS(i);
}
}
int main()
{
citire();
rezolvare();
fout<<insule;
return 0;
}