Pagini recente » Borderou de evaluare (job #3367318) | Cod sursa (job #3367342) | Cod sursa (job #3367327) | Cod sursa (job #3367178) | Cod sursa (job #3367112)
#include <bits/stdc++.h>
using namespace std;
const int MAXIM = 100001;
vector<int> graf[MAXIM];
ifstream fin("bfs.in");
ofstream fout("bfs.out");
bool visited[MAXIM];
size_t cate;
static inline void bfs(int start) {
queue<int> q;
q.push(start);
while (!q.empty()) {
const int current = q.front();
q.pop();
for (const auto& vecin : graf[current]) {
if (visited[vecin]) {
continue;
}
visited[vecin] = true;
q.push(vecin);
}
}
}
int main() {
int n, m;
fin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
fin >> x >> y;
graf[x].push_back(y);
graf[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (visited[i]) {
continue;
}
bfs(i);
cate++;
}
fout << cate;
}