#P9369. [ICPC 2022 Xi'an R] Tree

[ICPC 2022 Xi'an R] Tree

题目描述

You are given a tree TT with nn nodes. The tree is rooted at 11. Define subtree(u)\mathrm{subtree}(u) as the set of nodes in the subtree of uu.

Call a subset of nodes SS good if and only if SS satisfies at least one of the following contidions:

  • For all u,vSu, v\in S where uvu\neq v, either usubtree(v)u\in \mathrm{subtree}(v) or vsubtree(u)v\in \mathrm{subtree}(u).
  • For all u,vSu, v\in S where uvu\neq v, both usubtree(v)u\notin \mathrm{subtree}(v) and vsubtree(u)v\notin \mathrm{subtree}(u).

You need to partition all nodes of TT into several good subsets. Calculate the minimum number of subsets.

输入格式

The first line contains a single integer QQ (1Q1051\leq Q\leq 10 ^ 5), denoting the number of test cases.

For each test case, the first line contains an integer nn (1n1061\leq n\leq 10 ^ 6). The next line contains n1n - 1 integers p2,p3,,pnp_2, p_3, \ldots, p_n (1pi<i1\leq p_i < i), indicating that there is an edge between pip_i and ii for each i=2,3,,ni=2,3,\ldots,n.

It is guaranteed that the sum of nn over all test cases is no more than 10610 ^ 6.

输出格式

For each test case, output a single integer representing the answer.

2
7
1 1 2 2 2 3
5
1 2 3 4

3
1

提示

Source: The 2022 ICPC Asia Xi'an Regional Contest Problem L.

Author: Alex_Wei.