Language: C++

#include <iostream>
using namespace std;

#define MAX_N 100002

/* union find */
int par[MAX_N]; // root
int rank[MAX_N]; // depth of tree

// 要素数nで初期化
void init(int n) {
for (int i=0;i<n; i++) {
    par[i] = i;
    rank[i] = 0;
    }
}

// 木の根を求める(あえて根の張りなおしをしない)
int find(int x) {
    if (par[x] == x) {
        return x;
    } else {
        return find(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x==y) return;

    if(rank[x] < rank[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rank[x] == rank[y]) rank[x]++;
    }
}

int N,Q, marked[MAX_N];

int find_nearest_idx(int index) {
    if(marked[index]) return index;
    return find_nearest_idx(par[index]);
}

main() {
    while(1) {
        scanf("%d %d",&N,&Q);
        if(N==Q&&N==0) break;
        memset(marked,0,sizeof(marked));
        init(N+2);
        
        marked[1] = 1; // node 1 is already marked 
        for(int i=2;i<=N;i++) {
            int d;
            scanf("%d", &d);
            par[i] = d; // parent of i is d
            //unite(i,d);
        }

        // Question
        int ans = 0;
        for(int i=0;i<Q;i++) {
            char op;
            int index;
            cin >> op >> index;
            //scanf("%c %d", &op, &index);
            if(op == 'Q')
                ans += find_nearest_idx(index);
            else if(op == 'M')
                marked[index] = 1;
        }
        printf("%d\n", ans);
    }
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:14:5: error: reference to 'rank' is ambiguous
   14 |     rank[i] = 0;
      |     ^~~~
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: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100002]'
    8 | int rank[MAX_N]; // depth of tree
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:33:8: error: reference to 'rank' is ambiguous
   33 |     if(rank[x] < rank[y]) {
      |        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100002]'
    8 | int rank[MAX_N]; // depth of tree
      |     ^~~~
a.cc:33:18: error: reference to 'rank' is ambiguous
   33 |     if(rank[x] < rank[y]) {
      |                  ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100002]'
    8 | int rank[MAX_N]; // depth of tree
      |     ^~~~
a.cc:37:13: error: reference to 'rank' is ambiguous
   37 |         if (rank[x] == rank[y]) rank[x]++;
      |             ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100002]'
    8 | int rank[MAX_N]; // depth of tree
      |     ^~~~
a.cc:37:24: error: reference to 'rank' is ambiguous
   37 |         if (rank[x] == rank[y]) rank[x]++;
      |                        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100002]'
    8 | int rank[MAX_N]; // depth of tree
      |     ^~~~
a.cc:37:33: error: reference to 'rank' is ambiguous
   37 |         if (rank[x] == rank[y]) rank[x]++;
      |                                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100002]'
    8 | int rank[MAX_N]; // depth of tree
      |     ^~~~
a.cc: At global scope:
a.cc:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   48 | main() {
      | ^~~~
a.cc: In function 'int main()':
a.cc:52:9: error: 'memset' was not declared in this scope
   52 |         memset(marked,0,sizeof(marked));
      |         ^~~~~~
a.cc:2:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    1 | #include <iostream>
  +++ |+#include <cstring>
    2 | using namespace std;