#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
const ll mod = 1000000007;
const ll N = 20;
int a[N+2], dp[N+2], dep[N+2], sub[N+2], id[N+2][5], ord[N+2], near[N+2];
vector<int> adj[N+2], order;
struct Segtree{
    vector<int> t;
    int off = 1;
    Segtree(int n){
        while(off < n)off <<= 1;
        t.resize(2 * off, 0);
    };
    void update(int u, int v, int tp){
        t[u += off] = v;
        if(!tp)while(u >>= 1)t[u] = t[u * 2] + t[u * 2 + 1];
        else while(u >>= 1)t[u] = max(t[u * 2], t[u * 2 + 1]);
    }
    int query(int n, int ql, int qh, int nl, int nh, int tp){
        if(ql <= nl && nh <= qh)return t[n];
        if(nl > qh || ql > nh)return 0;
        int m = (nl + nh) / 2;
        if(!tp)return query(n * 2, ql, qh, nl, m, tp) + query(n * 2 + 1, ql, qh, m + 1, nh, tp);
        else return max(query(n * 2, ql, qh, nl, m, tp), query(n * 2 + 1, ql, qh, m + 1, nh, tp));
    }
};
void dfs(int u, int par, int d){
    dep[u] = d;
    sub[u] = 1;
    ord[u] = order.size();
    order.pb(u);
    for(auto &v : adj[u]){
        if(v == par)continue;
        dfs(v, u, d + 1);
        sub[u] += sub[v];
    }
}
int solve(int n, int m, std::vector<int> f, std::vector<std::vector<int>> s){
    cin.tie(0)->sync_with_stdio(0);
    for(int i = 1; i < n; i++){
        adj[f[i]].pb(i);
        adj[i].pb(f[i]);
    }
    dfs(0, 0, 0);
    vector<Segtree> segtree(m, Segtree(n));
    vector<Segtree> mid(m, Segtree(n));
    for(int j = 0; j < m; j++){
        for(int i = 0; i < n - 1; i++){
            id[s[j][i]][j] = i;
        }
    }
    stack<int> st;
    for(int i = n - 1; i >= 0; i--){
        while(!st.empty() && dep[order[st.top()]] > dep[order[i]])st.pop();
        if(st.empty())near[i] = i;
        else near[i] = st.top() - 1;
        st.push(i);
    }
    for(int j = 0; j < m; j++){
        for(int i = 0; i < n - 1; i++){
            mid[j].update(s[j][i], id[s[j][i]][j], 1);
        }
    }
    int togo = 0, cnt = 0;
    for(int i = 0; i < n - 1; i++){
        for(int j = 0; j < m; j++){
            segtree[j].update(s[j][i], 1, 0);
            int ans = segtree[j].query(1, ord[s[j][i]], near[ord[s[j][i]]], 0, segtree[j].off - 1, 0);
            if(ans != sub[s[j][i]]){
                int ans2 = mid[j].query(1, ord[s[j][i]], near[ord[s[j][i]]], 0, segtree[j].off - 1, 1);
                togo = max(togo, ans2);
            }
        }
        if(togo == i)cnt++, togo++;
    }
    return cnt;
}
// int main(){
//     int tt = 1;
//     //cin>>tt;
//     while(tt--){
//         cout<<solve(3, 1, {-1, 0, 0}, {{1, 2}});
//         cout<<'\n';
//     }
// }
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |