Pagini recente » Cod sursa (job #2901550) | Cod sursa (job #2097770) | Cod sursa (job #349315) | Cod sursa (job #1941968) | Cod sursa (job #2916595)
#include <iostream>
#include <fstream>
#include <vector>
#include <fstream>
using namespace std;
int n,m;
vector<int>graph[100005];
bool viz[100005];
void dfs(int node){
viz[node] = true;
for(auto x : graph[node])
if(!viz[x])
dfs(x);
}
int main() {
ifstream fin("dfs.in");
ofstream fout("dfs.out");
fin >> n >> m;
while(m--){
int x,y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int count = 0;
for(int i = 1;i <= n; ++i){
if(!viz[i]){
count ++;
dfs(i);
}
}
fout << count;
return 0;
}