template<typename T> inlinevoidread(T &x){ x = 0; T k = 1; char in = getchar(); while (!isdigit(in)) { if (in == '-') k = -1; in = getchar(); } while (isdigit(in)) x = x * 10 + in - '0', in = getchar(); x *= k; }
constint N = 1005;
structEdge { int nxt, to; };
int n, cnt; int head[N], f[N][5]; Edge e[N<<1];
inlinevoidadd(int u, int v){ e[++cnt] = (Edge){head[u], v}, head[u] = cnt; }
voiddfs(int x, int fa){ // 记录f[y][2], f[y][3]的总和,后面容斥即可 int sum2 = 0, sum3 = 0, y, tot; for (int i = head[x]; i; i = e[i].nxt) if (e[i].to != fa) { y = e[i].to; dfs(y, x); sum2 += f[y][2], sum3 += f[y][3]; tot++; } // 没有儿子特判 if (!tot) { f[x][0] = f[x][1] = f[x][2] = 1; return; } f[x][0] = 1, f[x][1] = f[x][2] = N; for (int i = head[x]; i; i = e[i].nxt) if (e[i].to != fa) { y = e[i].to; f[x][0] += f[y][4]; f[x][1] = min(f[x][1], f[y][0] + sum3 - f[y][3]); f[x][2] = min(f[x][2], f[y][1] + sum2 - f[y][2]); f[x][3] += f[y][2]; f[x][4] += f[y][3]; } // 检查最小值 for (int i = 1; i < 5; ++i) f[x][i] = min(f[x][i], f[x][i-1]); }
intmain(){ read(n); for (int i = 2, x; i <= n; ++i) read(x), add(x, i), add(i, x); dfs(1, 0); printf("%d\n", f[1][2]); }