Cod sursa(job #2930144)

Utilizator mati.coldea@gmail.comMatei Coldea [email protected] Data 27 octombrie 2022 16:31:05
Problema Parcurgere DFS - componente conexe Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>

using namespace std;
vector<int> G[10000];
vector<int> viz(1000,0);
int n, m;
int x, y;
int nrc = 0;

void dfs(int k, int nrc) {

	viz[k] = nrc;
	for (auto i : G[k]) {
		if (viz[i]==0) {
			dfs(i,nrc);
		}
	}

}

int main()
{
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}

	
	for (int i = 1; i <= n; i++) {
		if (viz[i] == 0) {
			nrc++;
			dfs(i, nrc);
		}
	}
	
	

	cout << nrc;

}