Language: C++

#include <iostream>
#include <vector>

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    int group_num() {
        return g_num;
    }
};

int main(void) {
    int n, q;
    cin >> n >> q;
    UnionFind un(n);
    for (int i = 0; i < q; i++) {
        int c, a, b;
        cin >> c >> a >> b;
        if (c == 0) un.unite(a, b);
        else cout << (un.same(a, b) ? 1 : 0) << endl;
    }
    return 0;
}

Compiler output:
a.cc:7:5: error: 'vector' does not name a type
    7 |     vector<int> rank, par;
      |     ^~~~~~
a.cc: In constructor 'UnionFind::UnionFind(int)':
a.cc:11:9: error: 'rank' was not declared in this scope; did you mean 'std::rank'?
   11 |         rank.resize(size, 0);
      |         ^~~~
      |         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:9: error: 'par' was not declared in this scope
   12 |         par.resize(size, 0);
      |         ^~~
a.cc: In member function 'void UnionFind::unite(int, int)':
a.cc:30:13: error: 'rank' was not declared in this scope; did you mean 'std::rank'?
   30 |         if (rank[x] > rank[y]) {
      |             ^~~~
      |             std::rank
/usr/include/c++/14/type_traits:1437:12: note: 'std::rank' declared here
 1437 |     struct rank
      |            ^~~~
a.cc:31:13: error: 'par' was not declared in this scope
   31 |             par[y] = x;
      |             ^~~
a.cc:33:13: error: 'par' was not declared in this scope
   33 |             par[x] = y;
      |             ^~~
a.cc: In member function 'int UnionFind::find(int)':
a.cc:39:18: error: 'par' was not declared in this scope
   39 |         if (x == par[x]) return x;
      |                  ^~~
a.cc:40:16: error: 'par' was not declared in this scope
   40 |         return par[x] = find(par[x]);
      |                ^~~
a.cc: In function 'int main()':
a.cc:50:5: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
   50 |     cin >> n >> q;
      |     ^~~
      |     std::cin
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:56:14: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   56 |         else cout << (un.same(a, b) ? 1 : 0) << endl;
      |              ^~~~
      |              std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:56:49: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   56 |         else cout << (un.same(a, b) ? 1 : 0) << endl;
      |                                                 ^~~~
      |                                                 std::endl
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~