The program finds the number of graph connectivity components using DSU. It is necessary to parallelize it into several cores. Any ideas? (I understand that it may be necessary to rewrite some functions from C ++ to C)
#include <iostream> #include <cstdlib> using namespace std; const int MaxSize = 1000; int rep[MaxSize], mult_size[MaxSize]; void MakeSet(int x) { rep[x] = x; mult_size[x] = 1; } int Find(int x) { if (rep[x] == x) return x; else return (rep[x] = Find(rep[x])); } void Unite(int x, int y) { x = Find(x); y = Find(y); if (x != y) { if (mult_size[x] > mult_size[y]) swap(x, y); rep[x] = y; mult_size[y] += mult_size[x]; } } int ConnectedComponentsAmount() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) MakeSet(i); int ver_1, ver_2; for (int i = 0; i < m; i++) { cin >> ver_1 >> ver_2; Unite(ver_1, ver_2); } bool used[MaxSize]; for (int i = 1; i <= n; i++) used[i] = false; for (int i = 1; i <= n; i++) used[Find(i)] = true; int count = 0; for (int i = 1; i <= n; i++) if (used[i]) count++; return count; } int main() { cout << ConnectedComponentsAmount(); return 0; }