Cod sursa(job #3367111)

Utilizator lefterache_stefanLefterache Stefan lefterache_stefan Data 6 octombrie 2026 10:48:23
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;

const int MAXIM = 100001;
vector<int> graf[MAXIM];

ifstream fin("dfs.in");
ofstream fout("dfs.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;
}