Pagini recente » Cod sursa (job #415887) | Cod sursa (job #2339738) | Cod sursa (job #2116951) | Cod sursa (job #896141) | Cod sursa (job #2501747)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int n, m, bodies;
vector<int> graph[100005];
int b[100005];
void dfs(int node){
for(auto &v : graph[node]){
if(b[v] == 0){
b[v] = bodies;
dfs(v);
}
}
}
int main() {
ifstream f("dfs.in");
ofstream g("dfs.out");
f>>n>>m;
for (int i = 0; i < m; ++i) {
int node, destination;
f>>node>>destination;
graph[node - 1].push_back(destination - 1);
graph[destination - 1].push_back(node - 1);
}
for (int i = 0; i < n; ++i) {
if(b[i] == 0){
++bodies;
dfs(i);
}
}
g<<bodies;
return 0;
}