Cod sursa(job #2427522)

Utilizator PrekzursilAndrei Visalon Prekzursil Data 31 mai 2019 23:07:59
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<bool> vis(100005, false);
vector<int> graph[100005];
void dfs(int v, vector<int> graph[]) {
	vis[v] = true;
	for (auto it : graph[v]) {
		if (vis[it] == false)
			dfs(it,graph);
	}
}
int main() {
	ifstream f("dfs.in");
	int n, m;
	f >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int a, b;
		f >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	f.close();
	int con = 0;
	for (int i = 1; i <= n; i++)
	{
		if (vis[i] == false)
		{
			con++;
			dfs(i, graph);
		}
	}
	ofstream g("dfs.out");
	g << con;
	g.close();
}