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;
    
    UF(int n):par(2*n),rank(2*n)
    {
        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 member function 'void UF::furiwake(int)':
a.cc:64:37: error: 'n' was not declared in this scope; did you mean 'yn'?
   64 |             if(m[x]) par[c[x][i]]=x+n;
      |                                     ^
      |                                     yn
a.cc: In member function 'int UF::score(int)':
a.cc:72:24: error: 'n' was not declared in this scope; did you mean 'yn'?
   72 |         return root(x)-n;
      |                        ^
      |                        yn
a.cc: In member function 'void UF::delmark(int)':
a.cc:77:19: error: break statement not within loop or switch
   77 |         if(!m[x]) break;
      |                   ^~~~~
a.cc:78:17: error: 'n' was not declared in this scope; did you mean 'yn'?
   78 |         unite(x+n,x);
      |                 ^
      |                 yn
a.cc: In function 'int main()':
a.cc:86:5: error: 'Uf' was not declared in this scope; did you mean 'UF'?
   86 |     Uf uf(n);
      |     ^~
      |     UF
a.cc:91:10: error: 'uf' was not declared in this scope; did you mean 'UF'?
   91 |         (uf.c[p]).push_back(i);
      |          ^~
      |          UF
a.cc:98:10: error: 'uf' was not declared in this scope; did you mean 'UF'?
   98 |         (uf.qu).push(P(temp,v-1));
      |          ^~
      |          UF
a.cc:98:24: error: cannot bind rvalue reference of type 'char&&' to lvalue of type 'char'
   98 |         (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:103:5: error: 'uf' was not declared in this scope; did you mean 'UF'?
  103 |     uf.furiwake(0);
      |     ^~
      |     UF
a.cc:108:26: error: 'qu' was not declared in this scope; did you mean 'q'?
  108 |         pair<char,int> a=qu.top();
      |                          ^~
      |                          q