Cod sursa(job #2460490)

Utilizator goldenpotato76Harangus Robert-Adrian goldenpotato76 Data 23 septembrie 2019 19:45:59
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int n, m;
vector<int> adj[100001];
bool viz[100001];

void dfs(int l) {
	viz[l] = true;

	for(int p: adj[l]) {
		if(!viz[p]) {
			viz[p] = true;
			dfs(p);
		}
	}
}

int main() {
	ifstream in("dfs.in");
	ofstream out("dfs.out");

	in >> n >> m;

	for(int i = 0; i < m; i++) {
		int x, y;
		in >> x >> y;

		adj[x].push_back(y);
		adj[y].push_back(x);
	}

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

	cout << c;
	out << c;

	in.close();
	out.close();
	return 0;
}