QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#5455. 树脚本

الإحصائيات

TreeScript 是一种用于维护树结构的编程语言。在本题中,我们将学习如何在 TreeScript 中创建有根树。

在 TreeScript 中,所有的树节点都存储在内存中。每个树节点都有一个编号及其父节点的地址,且两者都是不可变的,因此必须在创建节点时确定。特别地,根节点的父节点地址为空。

为了访问这些节点,节点的地址可以存储在寄存器中。如果有 $m$ 个寄存器,这些寄存器可以记为 $r[0], r[1], \dots, r[m - 1]$。

现在让我们学习节点创建语句:

$$r[i] = \text{create}(r[j], k);$$

其中 $k$ 是节点编号,$i$ 和 $j$ 是寄存器的索引,满足 $0 \le i, j < m$,且允许 $i = j$。该语句的作用是创建一个编号为 $k$ 的节点,其父节点地址存储在 $r[j]$ 中,然后将新节点的地址存储在 $r[i]$ 中。一旦每个节点都被正确创建,你就不再需要存储任何节点的地址;它们会自动执行预定义的指令。出于篇幅考虑,我们稍后再学习这些指令。

为了检验你的学习成果,你需要创建一个大小为 $n$ 的有根树。起初,系统会自动为你创建根节点并将其存储在 $r[0]$ 中。因此,你只需要执行 $n - 1$ 次额外的创建指令即可完成树的构建。

众所周知,寄存器非常昂贵,因此你需要找到所需的最小寄存器数量。

输入格式

输入包含多组测试数据。 第一行包含一个整数 $T$ ($1 \le T \le 10^5$),表示测试用例的数量。 对于每个测试用例: 第一行包含一个整数 $n$ ($2 \le n \le 2 \times 10^5$),表示树的大小。 第二行包含 $n$ 个整数 $p_1, p_2, \dots, p_n$,其中节点 $p_i$ 是节点 $i$ 的父节点,且满足 $1 \le p_i < i$。特别地,$p_1 = 0$,表示 $1$ 是树的根节点。 所有测试用例的 $n$ 之和不超过 $2 \times 10^5$。

输出格式

对于每个测试用例,在一行中输出答案。

样例

输入 1

2
3
0 1 2
7
0 1 2 2 1 4 1

输出 1

1
2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.