#include <bits/stdc++.h>
using namespace std;
vector<int> beechtree(int N, int M, vector<int> P, vector<int> C) {
vector<vector<int>> adj(N);
for (int i = 1; i < N; ++i) {
adj[P[i]].push_back(i);
}
vector<int> ans(N);
// colors, adj sizes
auto dfs = [&](auto &&self, int u) -> pair<map<int, int>, vector<int>> {
int max_e = 0;
map<int, int> cols;
vector<int> adjs = {int(adj[u].size())};
if (adjs[0] == 0) {
adjs.pop_back();
}
for (auto &i : adj[u]) {
cols[C[i]]++;
max_e = max(max_e, cols[C[i]]);
auto ch = self(self, i);
if (ch.first.size() > cols.size()) swap(ch.first, cols);
for (auto &[a, b] : ch.first) {
cols[a] += b;
max_e = max(max_e, cols[a]);
}
if (ch.second.size() > adjs.size()) swap(ch.second, adjs);
for (auto &sz : ch.second) {
adjs.push_back(sz);
}
}
vector<int> oth(max_e + 1);
for (auto &[a, b] : cols) {
oth[0]++, oth[b]--;
}
partial_sum(oth.begin(), oth.end(), oth.begin());
while (!oth.empty() && oth.back() == 0) {
oth.pop_back();
}
if (adjs.size() == oth.size()) {
sort(adjs.begin(), adjs.end());
sort(oth.begin(), oth.end());
ans[u] = adjs == oth;
}
return {cols, adjs};
};
dfs(dfs, 0);
return ans;
}