Cod sursa(job #2453541)

Utilizator davidcotigacotiga david davidcotiga Data 4 septembrie 2019 13:14:24
Problema Parcurgere DFS - componente conexe Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream> 
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

const int NLIM = 100005;

int N, M;

vector <int> Muchii[NLIM];
bool vizitat[NLIM];
int insule = 0;

void DFS(int Nod) {
	vizitat[Nod] = true;
	for (unsigned int i = 0; i < Muchii[Nod].size(); ++i) {
		int Vecin = Muchii[Nod][i];
		if (!vizitat[Vecin])
			DFS(Vecin);
	}
}

void citire() {
	fin >> N >> M;
	for (int i = 1; i <= N; ++i) {
		int x, y;
		fin >> x >> y;
		Muchii[x].push_back(y);
		Muchii[y].push_back(x);
	}
	for (int i = 1; i <= N; ++i) {
		if (!vizitat[i]) {
			insule += 1;
			DFS(i);
		}
	}
}


int main() {
	citire();
	fout << insule << "\n";
	return 0;
}