#P9369. [ICPC2022 Xi'an R] Tree

[ICPC2022 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.

题目大意

题目描述

给定大小为 nn 的有根树 TT,根节点为 11。定义 subtree(u)\mathrm{subtree}(u) 表示结点 uu 的子树。

称集合 SS 是好的,当且仅当 SS 至少满足下列条件之一:

  • 对于 u,vSu, v\in Suvu\neq vusubtree(v)u\in \mathrm{subtree}(v)vsubtree(u)v\in \mathrm{subtree}(u)
  • 对于 u,vSu, v\in Suvu\neq vusubtree(v)u\notin \mathrm{subtree}(v)vsubtree(u)v\notin \mathrm{subtree}(u)

TT 划分为若干好的集合,求集合数量的最小值。

共有 QQ 组数据。

1Q1051\leq Q\leq 10 ^ 51n,n1061\leq n, \sum n\leq 10 ^ 6,每个点的父亲编号 1pi<i1\leq p_i < i

输入格式

第一行一个整数 QQ

对于每组测试数据,第一行一个整数 nn,第二行 n1n - 1 个由空格隔开的整数 p2,p3,,pnp_2, p_3, \cdots, p_n,表示每个点的父亲编号。

输出格式

对于每组测试数据,输出一行一个整数表示答案。

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.