Pagini recente » Cod sursa (job #462278) | Cod sursa (job #563696) | Cod sursa (job #2683308) | Cod sursa (job #3268146) | Cod sursa (job #2597062)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
int n, m, s, visited[100005], x, y;
vector <int> graph[100005];
void dfs(int i) {
queue <int> depthSearch;
depthSearch.push(i);
visited[i] = 1;
while(depthSearch.size()) {
int adj = depthSearch.front();
depthSearch.pop();
for(int j = 0; j < graph[adj].size(); j++) {
int curr = graph[adj][j];
if(visited[curr] == 0) {
visited[curr] = 1;
depthSearch.push(curr);
}
}
}
}
int main()
{
in >> n >> m;
for(int i = 1; i <= m; i++) {
in >> x >> y;
graph[y].push_back(x);
graph[x].push_back(y);
}
int components = 0;
for(int i = 1; i <= n; i++) {
if(visited[i] == 0) {
dfs(i);
++components;
}
}
out << components;
return 0;
}