This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Ignut
started: 14.08.2024
now: 14.08.2024
████████████████████████████████████████████████████████████████████
████████████████████████████████    ████████████████████████████████
██████████████████████████████        ██████████████████████████████
██████      ██████████████████        ██████████████████      ██████
██████          ██████████████        ██████████████          ██████
██████      ██    ████████████        ████████████    ██      ██████
██████      ████    ██████████        ██████████    ████      ██████
██████      ████      ██████████    ██████████      ████      ██████
██████      ████      ██████████    ██████████    ██████      ██████
██████      ██████    ██████████    ██████████    ██████      ██████
██████      ██████    ████████        ████████    ██████      ██████
██████      ██████      ██████        ██████      ██████      ██████
██████      ████        ████            ████        ████      ██████
██████            ██████████    ████    ██████████            ██████
██████      ██      ██████    ████████    ██████      ██      ██████
██████      ██████            ████████            ██████      ██████
██████                    ██            ██                    ██████
██████████████████████      ████    ████      ██████████████████████
████████████████████████      ██    ██      ████████████████████████
██████████████████████████                ██████████████████████████
██████████████████████████████        ██████████████████████████████
████████████████████████████████████████████████████████████████████
*/
 
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
const int MAXN = 2e5 + 123;
int n;
vector<int> tree[MAXN];
vector<int> p;
vector<int> c;
vector<int> res;
int cnt[MAXN] = {};
bool Check(vector<int> &lst, int root) {
    // if (root == 4) {
    //     cout << root << " :\n";
    // }
    sort(lst.begin(), lst.end());
    do {
        bool ok = true;
        for (int v : lst) {
            int par = cnt[c[v]];
            int vert = (par == 0 ? root : lst[par - 1]);
            // if (root == 4) {
            //     cout << v << " -> " << par << ", ";
            // }
            if (p[v] != vert) {
                ok = false; break;
            }
            cnt[c[v]] ++;
        }
        // if (root == 4) {
        //     cout << '\n';
        // }
        for (int v : lst) cnt[c[v]] = 0;
        if (ok) return true;
    } while (next_permutation(lst.begin(), lst.end()));
    return false;
}
vector<int> dfs(int v) {
    vector<int> lst = {};
    for (int to : tree[v])
        if (to != p[v])
            for (int u : dfs(to))
                lst.push_back(u);
    res[v] |= Check(lst, v);
    lst.push_back(v);
    return lst;
}
vector<int> beechtree(int N, int M, vector<int> P, vector<int> C) {
    n = N; p = P; c = C;
    res.assign(N, 0);
    for (int i = 1; i < N; i ++) {
        tree[i].push_back(P[i]);
        tree[P[i]].push_back(i);
    }
    dfs(0);
    return res;
}
| # | 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... |