Cod sursa(job #3123679)

Utilizator IVVladIon Vlad Vasile IVVlad Data 25 aprilie 2023 09:03:41
Problema Parcurgere DFS - componente conexe Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

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

void read_input()
{

}

void write_output()
{

}

void solve(vector<vector<int>> graf, vector<int> &visited, int nod_curent)
{
	for(int j = 0; j < graf[nod_curent].size(); j++) {
		if(!visited[graf[nod_curent][j]]) {
			visited[graf[nod_curent][j]] = 1;
			solve(graf, visited, graf[nod_curent][j]);
		}
	}
}



int main()
{
	int n;
	int m;
	int x, y;

	f >> n >> m;
	vector<vector <int>> graf(n + 1);
	vector<int> visited(n + 1, 0);


	for(int i = 0; i < m; i++) {
		f >> x >> y;
		graf[x].push_back(y);
	}
	int nmb = 0;

	for(int i = 1; i <= n; i++) {
		if(!visited[i]) {
			solve(graf, visited, i);
			nmb++;
		}
	}

	g<<nmb;
}