Pagini recente » Cod sursa (job #2169165) | Monitorul de evaluare | Profil mihaistamatescu | Cod sursa (job #311840) | Cod sursa (job #1318096)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
const int nmax=100010;
int N,M;
bool poz[nmax];
vector<int> v[nmax];
void citire();
void conex(int);
int DFS();
void afisare();
void citire()
{
int x,y;
ifstream fin("dfs.in");
fin>>N>>M;
while(M--)
{
fin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
fin.close();
}
void conex(int j)
{
poz[j]=true;
for(vector<int>::iterator it=v[j].begin();it!=v[j].end();++it)
if(poz[*it]==false)
conex(*it);
}
int DFS()
{
int nr=0;
for(int i=1;i<=N;++i)
if(poz[i]==false)
{
nr++;
conex(i);
}
return nr;
}
void afisare()
{
ofstream fout("dfs.out");
fout<<DFS();
fout.close();
}
int main()
{
citire();
afisare();
return 0;
}