Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
//union find
//DSU Disjoint Set Union
class DSU {
public:
	vector<int> parent;

	DSU(int _size) : size(_size) {
		rank = vector<int>(size + 1, 1);
		parent = vector<int>(size + 1, 0);
		for(int i = 1; i < size + 1; i++)
		{
			parent[i] = i;
		}
	}

	int find(int i) {
		int r = i;
		while(parent[r] != r)
		{
			r = parent[r];
		}
		int temp;
		while(parent[i] != i)
		{
			temp = parent[i];
			parent[i] = r;
			rank[i] = 2;
			i = temp;
		}
		return parent[i];
	}

	bool Union(int i, int j) {
		int ri = find(i), rj = find(j);
		if(ri == rj) return false;
		if(rank[ri] > rank[rj])
		{
			parent[rj] = ri;
		}
		else if (rank[ri] < rank[rj])
		{
			parent[ri] = rj;
		}
		else
		{
			parent[rj] = ri;
			rank[ri] += 1;
		}
		return true;
	}

private:
	int size;
	vector<int> rank;
};

using namespace std;
int main()
{
	int N, M;
	cin >> N >> M;
	vector<int> X(M), Y(M), Z(M);
	for(int i = 0; i < M; i++)
	{
		cin >> X[i] >> Y[i] >> Z[i];
	}

	DSU dsu(N);
	for(int i = 0; i < M; i++)
	{
		dsu.Union(X[i], Y[i]);
	}
	unordered_set<int> s;
	for(int i = 1; i <= N; i++)
	{
		s.insert(dsu.parent[i]);
	}

	cout << s.size() << endl;
	return 1;
}


Compiler output:
a.cc:9:9: error: 'vector' does not name a type
    9 |         vector<int> parent;
      |         ^~~~~~
a.cc:58:9: error: 'vector' does not name a type
   58 |         vector<int> rank;
      |         ^~~~~~
a.cc: In constructor 'DSU::DSU(int)':
a.cc:12:17: error: 'rank' was not declared in this scope; did you mean 'std::rank'?
   12 |                 rank = vector<int>(size + 1, 1);
      |                 ^~~~
      |                 std::rank
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: 'std::rank' declared here
 1437 |     struct rank
      |            ^~~~
a.cc:12:24: error: 'vector' was not declared in this scope
   12 |                 rank = vector<int>(size + 1, 1);
      |                        ^~~~~~
a.cc:12:24: note: suggested alternatives:
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_vector.h:428:11: note:   'std::vector'
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
/usr/include/c++/14/vector:93:13: note:   'std::pmr::vector'
   93 |       using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
      |             ^~~~~~
a.cc:12:31: error: expected primary-expression before 'int'
   12 |                 rank = vector<int>(size + 1, 1);
      |                               ^~~
a.cc:13:17: error: 'parent' was not declared in this scope
   13 |                 parent = vector<int>(size + 1, 0);
      |                 ^~~~~~
a.cc:13:33: error: expected primary-expression before 'int'
   13 |                 parent = vector<int>(size + 1, 0);
      |                                 ^~~
a.cc: In member function 'int DSU::find(int)':
a.cc:22:23: error: 'parent' was not declared in this scope
   22 |                 while(parent[r] != r)
      |                       ^~~~~~
a.cc:27:23: error: 'parent' was not declared in this scope
   27 |                 while(parent[i] != i)
      |                       ^~~~~~
a.cc:31:25: error: 'rank' was not declared in this scope; did you mean 'std::rank'?
   31 |                         rank[i] = 2;
      |                         ^~~~
      |                         std::rank
/usr/include/c++/14/type_traits:1437:12: note: 'std::rank' declared here
 1437 |     struct rank
      |            ^~~~
a.cc:34:24: error: 'parent' was not declared in this scope
   34 |                 return parent[i];
      |                        ^~~~~~
a.cc: In member function 'bool DSU::Union(int, int)':
a.cc:40:20: error: 'rank' was not declared in this scope; did you mean 'std::rank'?
   40 |                 if(rank[ri] > rank[rj])
      |                    ^~~~
      |                    std::rank
/usr/include/c++/14/type_traits:1437:12: note: 'std::rank' declared here
 1437 |     struct rank
      |            ^~~~
a.cc:42:25: error: 'parent' was not declared in this scope
   42 |                         parent[rj] = ri;
      |                         ^~~~~~
a.cc:46:25: error: 'parent' was not declared in this scope
   46 |                         parent[ri] = rj;
      |                         ^~~~~~
a.cc:50:25: error: 'parent' was not declared in this scope
   50 |                         parent[rj] = ri;
      |                         ^~~~~~
a.cc: In function 'int main()':
a.cc:80:30: error: 'class DSU' has no member named 'parent'
   80 |                 s.insert(dsu.parent[i]);
      |                              ^~~~~~