Language: C++

#include <algorithm>
#include <string>

using namespace std;

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main() {
    int N, Q;
    cin >> N >> Q;

    UnionFind tree(N);

    for(int i = 0; i < Q; i++) {
        int p, x, y;
        cin >> p >> x >> y;
        if(p==0){
            tree.unite(x, y); // xの木とyの木を併合する
        }else{
            if(tree.same(x, y)){
                cout << "Yes" << endl;
            }else{
                cout << "No" << endl;
            }
        }
    }

    return 0;
}

Compiler output:
a.cc:7:5: error: 'vector' does not name a type
    7 |     vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
      |     ^~~~~~
a.cc: In constructor 'UnionFind::UnionFind(int)':
a.cc:9:24: error: class 'UnionFind' does not have any field named 'par'
    9 |     UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
      |                        ^~~
a.cc:10:36: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   10 |         for(int i = 0; i < N; i++) par[i] = i;
      |                                    ^~~
      |                                    __pstl::execution::v1::par
In file included from /usr/include/c++/14/pstl/glue_algorithm_defs.h:15,
                 from /usr/include/c++/14/algorithm:86,
                 from a.cc:1:
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In member function 'int UnionFind::root(int)':
a.cc:14:13: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   14 |         if (par[x] == x) return x;
      |             ^~~
      |             __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:15:16: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   15 |         return par[x] = root(par[x]);
      |                ^~~
      |                __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In member function 'void UnionFind::unite(int, int)':
a.cc:22:9: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   22 |         par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
      |         ^~~
      |         __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In function 'int main()':
a.cc:34:5: error: 'cin' was not declared in this scope
   34 |     cin >> N >> Q;
      |     ^~~
a.cc:3:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    2 | #include <string>
  +++ |+#include <iostream>
    3 | 
a.cc:45:17: error: 'cout' was not declared in this scope
   45 |                 cout << "Yes" << endl;
      |                 ^~~~
a.cc:45:17: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:45:34: error: 'endl' was not declared in this scope
   45 |                 cout << "Yes" << endl;
      |                                  ^~~~
a.cc:3:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    2 | #include <string>
  +++ |+#include <ostream>
    3 | 
a.cc:47:17: error: 'cout' was not declared in this scope
   47 |                 cout << "No" << endl;
      |                 ^~~~
a.cc:47:17: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:47:33: error: 'endl' was not declared in this scope
   47 |                 cout << "No" << endl;
      |                                 ^~~~
a.cc:47:33: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'