1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include <bits/stdc++.h> using namespace std; const int N = 1001, M = 2002; int u, h[N], e[M], ne[M], idx; int n, ans = N, s[N]; void add(int u, int v) { e[++idx] = v, ne[idx] = h[u], h[u] = idx; } int dfs(int u, int f) { int res = 0, idx = 0, c[999] = {0}; for (int i = h[u]; i; i = ne[i]) { int v = e[i]; if (v == f) continue; c[idx++] = dfs(v, u); } sort(c, c + idx, greater<int>()); for (int i = 0; i < idx; ++i) res = max(res, c[i] + i); return res + 1; } int main() { scanf("%d", &n); for (int i = 2; i <= n; ++i) { scanf("%d", &u); add(u, i), add(i, u); } for (int i = 1; i <= n; ++i) { s[i] = dfs(i, 0); ans = min(ans, s[i]); } printf("%d\n", ans); for (int i = 1; i <= n; ++i) if (s[i] == ans) printf("%d ", i); puts(""); return 0; }
|