Cod sursa(job #2377701)

Utilizator raul044Moldovan Raul raul044 Data 10 martie 2019 21:21:29
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>
#include <vector>

#define MAXN 100000

using namespace std;

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

int n, m, V[MAXN], count;
vector <int> G[MAXN]; 

void citire () {
	fin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int x, y;
		fin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
}

void DFS (int nod) {
	V[nod]++;
	int size = G[nod].size();
	for (int i = 0; i < size; ++i) {
		if (V[G[nod][i]] == 0)
			DFS(G[nod][i]);
	}
}

int main () {
	citire();
	for (int i = 1; i <= n; ++i) {
		if (V[i] == 0) {
			DFS(i);
			count++;
		}
	}
	fout << count;
}