Language: C++

//
//  2170.cpp
//  
//
//  Created by Yoshida Satoshi on 2018/10/28.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <utility>

#define MAX 100010
#define P make_pair<char,int>
typedef long long ll;

using namespace std;

struct UF
{
    vector<int> par;
    vector<int> rank;
    bool m[MAX];
    vector<int> c[MAX];
    stack<pair<char,int> > qu;
    int n;
    
    UF(int m):par(2*m),rank(2*m)
    {
        n=m;
        for(int i=0;i<2*n;i++) par[i]=i;
        for(int i=0;i<2*n;i++) rank[i]=0;
        for(int i=0;i<n;i++)   m[i]=false;
    }
    
    int root(int x)
    {
        if(par[x]==x) return x;
        return par[x]=root(par[x]);
    }
    
    void unite(int x,int y)
    {
        int rx=root(x);
        int ry=root(y);
        
        par[rx]=ry;
    }
    
    bool same(int x,int y)
    {
        int rx=root(x);
        int ry=root(y);
        
        return rx==ry;
    }
    
    void furiwake(int x=0)
    {
        for(int i=0;i<c[x].size();i++)
        {
            if(m[x]) par[c[x][i]]=x+n;
            else     par[c[x][i]]=x;
            furiwake(c[x][i]);
        }
    }
    
    int score(int x)
    {
        return root(x)-n;
    }
    
    void delmark(int x)
    {
        if(!m[x]) break;
        unite(x+n,x);
    }
};

int main()
{
    int n,q;
    cin>>n>>q;
    Uf uf(n);
    for(int i=1;i<n;i++)
    {
        int p;
        cin>>p;
        (uf.c[p]).push_back(i);
    }
    for(int i=0;i<q;i++)
    {
        char temp;
        int v;
        cin>>temp>>v;
        (uf.qu).push(P(temp,v-1));
        if(temp=='M') uf.m[v-1]=true;
    }
    int a,b;
    cin>>a>>b;
    uf.furiwake(0);
    
    ll ans=0;
    for(int i=0;i<q;i++)
    {
        pair<char,int> a=qu.top();
        qu.pop();
        if(a.first=='Q')
        {
            ans+=uf.score(a.second);
        }
        else
        {
            uf.delmark(a.second);
        }
    }
    
    cout<<ans<<endl;
    
    
    return 0;
}





Compiler output:
a.cc: In constructor 'UF::UF(int)':
a.cc:37:33: error: invalid types 'int[int]' for array subscript
   37 |         for(int i=0;i<n;i++)   m[i]=false;
      |                                 ^
a.cc: In member function 'void UF::delmark(int)':
a.cc:79:19: error: break statement not within loop or switch
   79 |         if(!m[x]) break;
      |                   ^~~~~
a.cc: In function 'int main()':
a.cc:88:5: error: 'Uf' was not declared in this scope; did you mean 'UF'?
   88 |     Uf uf(n);
      |     ^~
      |     UF
a.cc:93:10: error: 'uf' was not declared in this scope; did you mean 'UF'?
   93 |         (uf.c[p]).push_back(i);
      |          ^~
      |          UF
a.cc:100:10: error: 'uf' was not declared in this scope; did you mean 'UF'?
  100 |         (uf.qu).push(P(temp,v-1));
      |          ^~
      |          UF
a.cc:100:24: error: cannot bind rvalue reference of type 'char&&' to lvalue of type 'char'
  100 |         (uf.qu).push(P(temp,v-1));
      |                        ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:9:
/usr/include/c++/14/bits/stl_pair.h:1132:21: note:   initializing argument 1 of 'constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = char; _T2 = int; typename __strip_reference_wrapper<typename decay<_Tp>::type>::__type = char; typename decay<_Tp>::type = char; typename __strip_reference_wrapper<typename decay<_Tp2>::type>::__type = int; typename decay<_Tp2>::type = int]'
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |               ~~~~~~^~~
a.cc:105:5: error: 'uf' was not declared in this scope; did you mean 'UF'?
  105 |     uf.furiwake(0);
      |     ^~
      |     UF
a.cc:110:26: error: 'qu' was not declared in this scope; did you mean 'q'?
  110 |         pair<char,int> a=qu.top();
      |                          ^~
      |                          q