Cod sursa(job #2379541)

Utilizator gabawrrGabor Vlad-Sebastian gabawrr Data 13 martie 2019 20:08:52
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <fstream>
#include <vector>
#define MAX 100005

using namespace std;

ifstream f("dfs.in");
ofstream o("dfs.out");

int n, m, s, c;
int beenThere[MAX];
vector<int> graf[MAX];

void dfs(int start)
{
	beenThere[start] = 1;
	for (int i = 0; i < graf[start].size(); i++)
	{
		int next = graf[start][i];
		if (!beenThere[next])
			dfs(next);
	}
}

int main()
{
	f >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int x, y;
		f >> x >> y;
		graf[x].push_back(y);
		graf[y].push_back(x);
	}

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