Cod sursa(job #3367122)

Utilizator lefterache_stefanLefterache Stefan lefterache_stefan Data 6 octombrie 2026 10:58:38
Problema Parcurgere DFS - componente conexe Scor 45
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 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];

static inline void dfs(int nod) {
	visited[nod] = true;
	for (const auto& vecin : graf[nod]) {
		if (visited[vecin]) {
			continue;
		}
		dfs(vecin);
	}
}

int main() {
	int start, n, m;
	fin >> n >> m >> start;

	for (int i = 0; i < m; i++) {
		int x, y;
		fin >> x >> y;
		graf[x].push_back(y);
		graf[y].push_back(x);
	}
	size_t cate = 0;
	for (int i = 1; i <= n; i++) {
		if (visited[i]) {
			continue;
		}
		cate++;
		dfs(i);
	}
	fout << cate;
}