Language: C++

#include<iostream>
#include<vector>
struct UnionFind{
    vector <int> par;
    UnionFind(int N) : par(N, -1){}
    void unite(int x, int y){
        x = root(x); y = root(y);
        if(x != y){
            par[x] += par[y];
            par[y] = x;
        }
    }
    int root(int x){ return par[x] < 0 ? x : par[x] = root(par[x]);}
    bool same(int x, int y){ return root(x) == root(y);}
};

typedef pair<bool, int> pbt;;
bool marked[100000];
int p[100000];
int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    int N, Q, _t;
    char _c;
    while(ifs >> N >> Q && N){
        memset(marked, 0, sizeof(marked));
        marked[0] = true;
        UnionFind uf(N);
        vector<pbt> query;
        
        REP(i, 1, N){
            ifs >> p[i]; --p[i];
        }
        rep(i, Q){
            ifs >> _c >>_t; --_t;
            if(_c == 'Q')
              query.push_back(pbt(1, _t));
            else if(!marked[_t]){
                query.push_back(pbt(0, _t));
                marked[_t] = true;
            }
        }
        rep(i, N)
          if(!marked[i])
            uf.unite(p[i], i);
        ll sum = 0LL;
        for(int i = query.size() - 1; i >= 0; --i){
            int v = query[i].second;
            if(query[i].first)
              sum += uf.root(v) + 1;
            else
              uf.unite(p[v], v);
        }
        cout << sum << endl;
    }
    return 0;
}

Compiler output:
a.cc:4:5: error: 'vector' does not name a type
    4 |     vector <int> par;
      |     ^~~~~~
a.cc: In constructor 'UnionFind::UnionFind(int)':
a.cc:5:24: error: class 'UnionFind' does not have any field named 'par'
    5 |     UnionFind(int N) : par(N, -1){}
      |                        ^~~
a.cc: In member function 'void UnionFind::unite(int, int)':
a.cc:9:13: error: 'par' was not declared in this scope
    9 |             par[x] += par[y];
      |             ^~~
a.cc: In member function 'int UnionFind::root(int)':
a.cc:13:29: error: 'par' was not declared in this scope
   13 |     int root(int x){ return par[x] < 0 ? x : par[x] = root(par[x]);}
      |                             ^~~
a.cc: At global scope:
a.cc:17:9: error: 'pair' does not name a type
   17 | typedef pair<bool, int> pbt;;
      |         ^~~~
a.cc: In function 'int main()':
a.cc:21:5: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
   21 |     cin.tie(0); ios::sync_with_stdio(false);
      |     ^~~
      |     std::cin
In file included from a.cc:1:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:21:17: error: 'ios' has not been declared
   21 |     cin.tie(0); ios::sync_with_stdio(false);
      |                 ^~~
a.cc:24:11: error: 'ifs' was not declared in this scope
   24 |     while(ifs >> N >> Q && N){
      |           ^~~
a.cc:25:9: error: 'memset' was not declared in this scope
   25 |         memset(marked, 0, sizeof(marked));
      |         ^~~~~~
a.cc:3:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    2 | #include<vector>
  +++ |+#include <cstring>
    3 | struct UnionFind{
a.cc:28:9: error: 'vector' was not declared in this scope
   28 |         vector<pbt> query;
      |         ^~~~~~
a.cc:28:9: note: suggested alternatives:
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:2:
/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:28:16: error: 'pbt' was not declared in this scope
   28 |         vector<pbt> query;
      |                ^~~
a.cc:28:21: error: 'query' was not declared in this scope
   28 |         vector<pbt> query;
      |                     ^~~~~
a.cc:30:13: error: 'i' was not declared in this scope
   30 |         REP(i, 1, N){
      |             ^
a.cc:30:9: error: 'REP' was not declared in this scope
   30 |         REP(i, 1, N){
      |         ^~~
a.cc:33:9: error: 'rep' was not declared in this scope
   33 |         rep(i, Q){
      |         ^~~
a.cc:45:9: error: 'll' was not declared in this scope
   45 |         ll sum = 0LL;
      |         ^~
a.cc:49:15: error: 'sum' was not declared in this scope
   49 |               sum += uf.root(v) + 1;
      |               ^~~
a.cc:53:9: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   53 |         cout << sum << 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:53:17: error: 'sum' was not declared in this scope
   53 |         cout << sum << endl;
      |                 ^~~
a.cc:53:24: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   53 |         cout << sum << endl;
      |                        ^~~~
      |                        std::endl
In file included from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~