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

[ICPC 2022 Xi'an R] Tree

[ICPC 2022 Xi'an R] Tree

题目描述

You are given a tree $T$ with $n$ nodes. The tree is rooted at $1$. Define $\mathrm{subtree}(u)$ as the set of nodes in the subtree of $u$.

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

  • For all u,v∈Su, v\in S where u≠vu\neq v, either u∈subtree(v)u\in \mathrm{subtree}(v) or v∈subtree(u)v\in \mathrm{subtree}(u).
  • For all u,v∈Su, v\in S where u≠vu\neq v, both u∉subtree(v)u\notin \mathrm{subtree}(v) and v∉subtree(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 $Q$ ($1\leq Q\leq 10 ^ 5$), denoting the number of test cases.

For each test case, the first line contains an integer nn (1≤n≤1061\leq n\leq 10 ^ 6). The next line contains n−1n - 1 integers p2,p3,…,pnp_2, p_3, \ldots, p_n (1≤pi<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. 标签: P9369|2022|O2优化|ICPC|西安

来源

P9369|[ICPC 2022 Xi'an R] Tree