Pagini recente » Cod sursa (job #1965913) | Cod sursa (job #1425862) | Cod sursa (job #2741117) | Cod sursa (job #3244700) | Cod sursa (job #2660320)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int NLIM = 100005;
bool vizitat[NLIM];
int N , M;
int grupe = 0;
vector < int > Muchii[NLIM];
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()
{
f>>N>>M;
for(int i=1;i<=M;i++)
{
int x , y;
f>>x>>y;
Muchii[x].push_back(y);
Muchii[y].push_back(x);
}
for(int i=1;i<=N;i++)
{
if(!vizitat[i])
{
grupe++;
DFS(i);
}
}
}
int main()
{
citire();
g<<grupe;
return 0;
}