Pagini recente » Cod sursa (job #1383429) | Istoria paginii runda/ah1/clasament | Cod sursa (job #2903829) | Cod sursa (job #1145046) | Cod sursa (job #2308697)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,k;
const int LimNoduri = 10000;
vector <int> edge[LimNoduri];
bool vizitat[LimNoduri];
void DFS(int Nod)
{
vizitat[Nod] = true;
for(unsigned int i=0; i<edge[Nod].size();i++){
int next = edge[Nod][i];
if(!vizitat[next]){
DFS(next);
}
}
}
void citire()
{
fin>>n>>m;
int x,y;
for(int i=1;i<=m;i++){
fin>>x>>y;
edge[x].push_back(y);
edge[y].push_back(x);
}
}
int main()
{
citire();
for(int i=1;i<=n;i++){
if(!vizitat[i]){
k++;
DFS(i);
}
}
fout<<k;
return 0;
}