AtCoder Regular Contest 205 (Div. 2)
D
Non-Ancestor Matching
AtCoder
⌀ 1853
原题
← CE →
翻译中…

Problem Statement

翻译中…

You are given a rooted tree with $N$ vertices numbered from $1$ to $N$. Vertex $1$ is the root, and the parent of vertex $i$ $(2 \le i \le N)$ is vertex $p_i$ $(1\le p_i < i)$. Initially, all vertices are colored white.

翻译中…

You can perform the following sequence of operations zero or more times.

翻译中…
  • Choose a pair of integers $(u,v)$ that satisfies all of the following conditions.
  • $1\le u < v \le N$
    • Both vertices $u$ and $v$ are colored white.
    • $u$ is not an ancestor of $v$.
  • Color vertices $u$ and $v$ black.
翻译中…

Here, "$u$ is not an ancestor of $v$" means that you cannot reach vertex $u$ from vertex $v$ no matter how many times you perform the operation of moving to the parent of the current vertex.

翻译中…

Find the maximum number of operations you can perform when you perform operations in an appropriate order.

翻译中…

You are given $T$ test cases, so find the answer for each of them.

翻译中…

Constraints

翻译中…
  • $1\le T\le 5\times 10^4$
  • $2\le N\le 5\times 10^5$
  • $1\le p_i < i$
  • The sum of $N$ over all test cases is at most $5\times 10^5$.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

翻译中…

Each test case $\text{case}_i$ is given in the following format:

翻译中…

N
p_2 p_3 \ldots p_N

翻译中…

Output

翻译中…

Output $T$ lines.

翻译中…

The $i$-th line $(1\le i\le T)$ should contain the answer for the $i$-th test case.

翻译中…

样例

Input #1
4
6
1 2 2 2 5
7
1 2 3 4 5 6
7
1 2 3 4 2 4
12
1 1 2 2 2 4 4 4 7 7 7
Output #1
2
0
2
5
Consider the first test case. ![image](https://img.atcoder.jp/arc205/6734339acfecc9c4c7ae80a268dda726.png) You can perform two operations as follows. - Choose $(u,v)=(3,5)$. Color vertices $3$ and $5$ black. - Choose $(u,v)=(4,6)$. Color vertices $4$ and $6$ black. You cannot perform more than two operations, so output $2$ on the first line.
题目信息
题号
arc205_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。