#include <bits/stdc++.h> // NeOWami
using namespace std;
#define ft first
#define sc second
using pii = pair<int, int>;
const int N = 1e5 + 5;
int n, k;
bool onColor[N];
vector<int> G[N];
int W[N], par[N], h[N], tin[N], tout[N], tour[N], stt = 0, ans = 0, tans = 0, lim = 0;
vector<int> Color[N], Depth[N];
map<int, int> cnt[N];
int cAtDep[N], cc[N];
int vis[N], vers = 0;
inline bool ckmax(int &u, int v) {
if (u < v) return u = v, 1;
return 0;
}
inline void calcans(int t1, int t2) {
if (ckmax(ans, t1)) tans = t2;
else if (ans == t1) tans = min(tans, t2);
}
namespace sub1 {
bool checksub() {
for (int i = 1; i <= n; i++) if ((int)G[i].size() > 1) return false;
return true;
}
void solve() {
map<int, int> cnt;
for (int i = 1; i <= n; i++) cnt[W[i]]++;
for (pii item: cnt) ans = max(ans, item.sc);
cout << ans << " 0";
}
};
void gettour(int u) {
tour[++stt] = u;
tin[u] = stt;
for (int v: G[u]) {
h[v] = h[u] + 1;
gettour(v);
}
tout[u] = stt;
Depth[h[u]].push_back(tout[u]);
lim = max(lim, h[u]);
}
namespace sub2 {
void dfs(int u, int root) {
cc[h[u]]++;
if (W[u] == W[root]) cAtDep[h[u]]++;
for (int v: G[u]) {
dfs(v, root);
}
}
void solve() {
for (int u = 1; u <= n; u++) {
dfs(u, u);
int val = 0, t = 0;
for (int i = h[u]; i <= lim; i++) {
int add = 0; if (cnt[i].count(W[u])) add = min(cc[i], cnt[i][W[u]]);
val += add;
t += add - cAtDep[i];
// cerr << val << " " << t << "| " << add << " " << cAtDep[i] << "\n";
cAtDep[i] = cc[i] = 0;
}
calcans(val, t);
}
cout << ans << " " << tans;
}
};
namespace sub3 {
bool checksub() {
for (int i = 1; i <= n; i++) if (Color[i].size() > 10) return false;
return true;
}
int getAtDep(int u, int h) {
int r = upper_bound(Depth[h].begin(), Depth[h].end(), tout[u]) - Depth[h].begin() - 1;
int l = lower_bound(Depth[h].begin(), Depth[h].end(), tin[u]) - Depth[h].begin();
return r - l + 1;
}
void solve() {
for (int c = 1; c <= k; c++) if (Color[c].size()) {
for (int u: Color[c]) {
vers++;
int val = 0, t = 0;
for (int v: Color[c]) {
if (tin[u] <= tin[v] && tout[v] <= tout[u]) cAtDep[h[v]]++;
}
for (int v: Color[c]) if (vis[h[v]] != vers) {
int d = h[v];
cc[d] = getAtDep(u, d);
// cerr << cc[d] << "\n";
int add = 0; if (cnt[d].count(c)) add = min(cc[d], cnt[d][c]);
val += add;
t += add - cAtDep[d];
// cerr << val << " " << t << "| " << add << " " << cAtDep[i] << "\n";
vis[d] = vers;
cAtDep[d] = cc[d] = 0;
}
calcans(val, t);
}
}
cout << ans << " " << tans;
}
}
namespace subfull {
int getAtDep(int u, int h) {
int r = upper_bound(Depth[h].begin(), Depth[h].end(), tout[u]) - Depth[h].begin() - 1;
int l = lower_bound(Depth[h].begin(), Depth[h].end(), tin[u]) - Depth[h].begin();
return r - l + 1;
}
void caseLight(int c) {
for (int u: Color[c]) {
vers++;
int val = 0, t = 0;
for (int v: Color[c]) {
if (tin[u] <= tin[v] && tout[v] <= tout[u]) cAtDep[h[v]]++;
}
for (int v: Color[c]) if (vis[h[v]] != vers) {
int d = h[v];
cc[d] = getAtDep(u, d);
int add = 0; if (cnt[d].count(c)) add = min(cc[d], cnt[d][c]);
val += add;
t += add - cAtDep[d];
// cerr << v << " " << h[v] << "\n";
vis[d] = vers;
cAtDep[d] = cc[d] = 0;
}
calcans(val, t);
}
}
void dfs(int u, int root) {
cc[h[u]]++;
vis[u] = vers;
lim = max(lim, h[u]);
if (W[u] == W[root]) cAtDep[h[u]]++;
for (int v: G[u]) {
dfs(v, root);
}
}
void caseHeavy(int c) {
vers++;
for (int i = 1; i <= stt; i++) if (vis[tour[i]] != vers && W[tour[i]] == c) {
int u = tour[i];
lim = 0;
// cerr << u << "\n";
dfs(u, u);
int val = 0, t = 0;
for (int i = h[u]; i <= lim; i++) {
int add = 0; if (cnt[i].count(c)) add = min(cc[i], cnt[i][c]);
val += add;
t += add - cAtDep[i];
cAtDep[i] = cc[i] = 0;
}
calcans(val, t);
}
}
void solve() {
for (int i = 1; i <= n; i++) if (Color[i].size()) {
if ((int)Color[i].size() <= sqrt(n)) {
caseLight(i);
}
else {
caseHeavy(i);
}
}
cout << ans << " " << tans;
}
}
signed main() {
cin.tie(NULL)->sync_with_stdio(false);
if(ifstream("OFFOFF.inp")) {
freopen("OFFOFF.inp", "r", stdin);
freopen("OFFOFF.out", "w", stdout);
}
cin >> n;
k = 0;
for (int i = 1; i <= n; i++) {
cin >> W[i];
if (!onColor[W[i]]) k++;
onColor[W[i]] = 1;
}
for (int v = 2; v <= n; v++) {
int u; cin >> u;
par[v] = u;
G[u].push_back(v);
}
gettour(1);
for (int i = 1; i <= n; i++) cnt[h[i]][W[i]]++;
for (int i = 1; i <= n; i++) Color[W[i]].push_back(i);
// if (sub1::checksub()) return sub1::solve(), 0;
if (n <= 2000) return sub2::solve(), 0;
if (sub3::checksub()) return sub3::solve(), 0;
return subfull::solve(), 0;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+IC8vIE5lT1dhbWkKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgZnQgZmlyc3QKI2RlZmluZSBzYyBzZWNvbmQKdXNpbmcgcGlpID0gcGFpcjxpbnQsIGludD47CmNvbnN0IGludCBOID0gMWU1ICsgNTsKaW50IG4sIGs7CmJvb2wgb25Db2xvcltOXTsKCnZlY3RvcjxpbnQ+IEdbTl07CmludCBXW05dLCBwYXJbTl0sIGhbTl0sIHRpbltOXSwgdG91dFtOXSwgdG91cltOXSwgc3R0ID0gMCwgYW5zID0gMCwgdGFucyA9IDAsIGxpbSA9IDA7CnZlY3RvcjxpbnQ+IENvbG9yW05dLCBEZXB0aFtOXTsKbWFwPGludCwgaW50PiBjbnRbTl07CmludCBjQXREZXBbTl0sIGNjW05dOwppbnQgdmlzW05dLCB2ZXJzID0gMDsKCmlubGluZSBib29sIGNrbWF4KGludCAmdSwgaW50I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gc29sdmUoKSB7CiAgICBmb3IgKGludCBjID0gMTsgYyA8PSBrOyBjKyspIGlmIChDb2xvcltjXS5zaXplKCkpIHsKICAgICAgICBmb3IgKGludCB1OiBDb2xvcltjXSkgewogICAgICAgICAgICB2ZXJzKys7CiAgICAgICAgICAgIGludCB2YWwgPSAwLCB0ID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgdjogQ29sb3JbY10pIHsKICAgICAgICAgICAgICAgIGlmICh0aW5bdV0gPD0gdGluW3ZdICYmIHRvdXRbdl0gPD0gdG91dFt1XSkgY0F0RGVwW2hbdl1dKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZm9yIChpbnQgdjogQ29sb3JbY10pIGlmICh2aXNbaFt2XV0gIT0gdmVycykgewogICAgICAgICAgICAgICAgaW50IGQgPSBoW3ZdOwogICAgICAgICAgICAgICAgY2NbZF0gPSBnZXRBdERlcCh1LCBkKTsKICAgICAgICAgICAgICAgIC8vIGNlcnIgPDwgY2NbZF0gPDwgIlxuIjsKICAgICAgICAgICAgICAgIGludCBhZGQgPSAwOyBpZiAoY250W2RdLmNvdW50KGMpKSBhZGQgPSBtaW4oY2NbZF0sIGNudFtkXVtjXSk7CiAgICAgICAgICAgICAgICB2YWwgKz0gYWRkOwogICAgICAgICAgICAgICAgdCArPSBhZGQgLSBjQXREZXBbZF07CiAgICAgICAgICAgICAgICAvLyBjZXJyIDw8IHZhbCA8PCAiICIgPDwgdCA8PCAifCAiIDw8IGFkZCA8PCAiICIgPDwgY0F0RGVwW2ldIDw8ICJcbiI7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIHZpc1tkXSA9IHZlcnM7CiAgICAgICAgICAgICAgICBjQXREZXBbZF0gPSBjY1tkXSA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY2FsY2Fucyh2YWwsIHQpOwogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgYW5zIDw8ICIgIiA8PCB0YW5zOwp9Cn0KCm5hbWVzcGFjZSBzdWJmdWxsIHsKaW50IGdldEF0RGVwKGludCB1LCBpbnQgaCkgewogICAgaW50IHIgPSB1cHBlcl9ib3VuZChEZXB0aFtoXS5iZWdpbigpLCBEZXB0aFtoXS5lbmQoKSwgdG91dFt1XSkgLSBEZXB0aFtoXS5iZWdpbigpIC0gMTsKICAgIGludCBsID0gbG93ZXJfYm91bmQoRGVwdGhbaF0uYmVnaW4oKSwgRGVwdGhbaF0uZW5kKCksIHRpblt1XSkgLSBEZXB0aFtoXS5iZWdpbigpOwogICAgcmV0dXJuIHIgLSBsICsgMTsKfQoKdm9pZCBjYXNlTGlnaHQoaW50IGMpIHsKICAgIGZvciAoaW50IHU6IENvbG9yW2NdKSB7CiAgICAgICAgdmVycysrOwogICAgICAgIGludCB2YWwgPSAwLCB0ID0gMDsKICAgICAgICBmb3IgKGludCB2OiBDb2xvcltjXSkgewogICAgICAgICAgICBpZiAodGluW3VdIDw9IHRpblt2XSAmJiB0b3V0W3ZdIDw9IHRvdXRbdV0pIGNBdERlcFtoW3ZdXSsrOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCB2OiBDb2xvcltjXSkgaWYgKHZpc1toW3ZdXSAhPSB2ZXJzKSB7CiAgICAgICAgICAgIGludCBkID0gaFt2XTsKICAgICAgICAgICAgY2NbZF0gPSBnZXRBdERlcCh1LCBkKTsKICAgICAgICAgICAgaW50IGFkZCA9IDA7IGlmIChjbnRbZF0uY291bnQoYykpIGFkZCA9IG1pbihjY1tkXSwgY250W2RdW2NdKTsKICAgICAgICAgICAgdmFsICs9IGFkZDsKICAgICAgICAgICAgdCArPSBhZGQgLSBjQXREZXBbZF07CiAgICAgICAgICAgIC8vIGNlcnIgPDwgdiA8PCAiICIgPDwgaFt2XSA8PCAiXG4iOwogICAgICAgICAgICB2aXNbZF0gPSB2ZXJzOwogICAgICAgICAgICBjQXREZXBbZF0gPSBjY1tkXSA9IDA7CiAgICAgICAgfQogICAgICAgIGNhbGNhbnModmFsLCB0KTsKICAgI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