int n; std::pair<int, std::vector<int>> e[MAXN + 2];
intdfs1(int p){ e[p].first = 1; if (e[p].second.size() == 0) return1; for (int each : e[p].second) e[p].first += dfs1(each); return e[p].first; }
voiddfs2(int p){ if (p != 0 && p != n + 1) printf("%d ", p); int max = 0; for (int each : e[p].second) max = std::max(max, e[each].first); for (int each : e[p].second) { if (e[each].first != max) { dfs2(each); } } for (int each : e[p].second) { if (e[each].first == max) { dfs2(each); } } }
voidsolve(){ staticint b[MAXN + 1];
scanf("%d", &n);
int k = 0; bool flag = true; for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); if (b[i] > i) k = i; if (b[i] == 0) flag = false; } printf("%d\n", k);
for (int i = 0; i <= n + 1; i++) e[i].second.clear(); for (int i = 1; i <= n; i++) e[b[i]].second.push_back(i);