Pagini recente » Cod sursa (job #774506) | Cod sursa (job #2485545) | Cod sursa (job #1475614) | Cod sursa (job #1336293) | Cod sursa (job #2713337)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int NLIM = 100005;
vector <int> muchii[NLIM];
bool check[NLIM];
void dfs(int nod){
check[nod] = 1;
for(unsigned int i=0; i<muchii[nod].size(); i++){
int next = muchii[nod][i];
if(check[next]==0)
dfs(next);
}
}
int main()
{
int n, m, insule = 0;
in >> n >> m;
for(int i=1; i<=m; i++){
int x, y;
in >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
for(int i=1; i<=n; i++){
if(check[i]==0){
// out << i << " ";
insule++;
dfs(i);
}
}
out << insule;
return 0;
}