Cod sursa(job #2677796)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 27 noiembrie 2020 15:33:27
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>

using namespace std;

#include <fstream>
//ifstream cin("input.in"); ofstream cout("output.out");
ifstream cin("dfs.in"); ofstream cout("dfs.out");

//VARIABLES

vector <int> gr[100005];
int used[100005];
int ans;

//FUNCTIONS

void dfs(int nod) {

	used[nod] = 1;

	for (auto& x : gr[nod]) {
		if (!used[x]) {
			dfs(x);
		}
	}

}

//MAIN

int main() {

	int n, m;
	cin >> n >> m;

	for (int i = 1; i <= m; i++) {
		int x, y;
		cin >> x >> y;

		gr[x].push_back(y);
		gr[y].push_back(x);
	}

	for (int i = 1; i <= n; i++) {
		if (!used[i]) {
			ans++;
			dfs(i);
		}
	}

	cout << ans << '\n';

	return 0;
}