Pagini recente » Istoria paginii utilizator/al3xand3r97 | Cod sursa (job #441879) | Cod sursa (job #363891) | Cod sursa (job #209320) | Cod sursa (job #1567227)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int nmax = 100050;
vector <int> graf[nmax];
int n, m, counter;
bool checked[nmax];
void dfs (int node){
checked[node] = true;
for(int i = 0; i < graf[node].size(); i++)
if(!checked[graf[node][i]])
dfs(graf[node][i]);
}
int main()
{
in >> n >> m;
for(int i = 0, x, y; i < m; i++){
in >> x >> y;
graf[x].push_back(y);
graf[y].push_back(x);
}
for(int i = 1; i <= n; i++){
if(!checked[i]){
dfs(i);
++counter;
}
}
out << counter << '\n';
return 0;
}