CF2238C.Village Guilds

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Hmmmrmm.

— Minecraft

During his long adventure, Steve stumbled upon a village of villagers. The houses in the village are connected by bidirectional paths. In total, there are nn houses in the village, numbered from 11 to nn. The graph of houses and paths represents a rooted∗^{\text{∗}} tree†^{\text{†}}, rooted at vertex 11, where the town hall is located.

Consider an arbitrary house vv and its subtree‡^{\text{‡}}. For each such house vv and each non-negative integer hh, consider the set of houses that are in the subtree of vv at a distance exactly hh from vv. Such a set will be called a guild. For example, for h=0h=0, the guild will consist only of the vertex vv.

In the example below, the guilds for the vertex v=4v=4 are shown. The guild for h=0h=0 is marked in red, for h=1h=1 in light blue, and for h=2h=2 in green.

Two guilds are considered different if there exists a house that is in one guild and not in the other. Steve wants to know how many different non-empty guilds there are in the tree. Help him with this.

∗^{\text{∗}}A rooted tree is a tree where one vertex is special and called the root.

†^{\text{†}}A tree is a connected graph without cycles.

‡^{\text{‡}}A subtree of vertex vv is the subgraph of vv, all its descendants, and all the edges between them.

Hmmmrmm。

——《我的世界》

在漫长的冒险途中,史蒂夫偶然发现了一个村民村庄。村庄中的房屋由双向路径连接。村庄中共有 nn 座房屋,编号从 11 到 nn。这些房屋与路径构成的图是一棵有根树∗^{\text{∗}},根节点为顶点 11(即市政厅所在地)。

考虑任意一座房屋 vv 及其子树‡^{\text{‡}}。对每个这样的房屋 vv 和每个非负整数 hh,定义集合:该集合包含所有位于 vv 的子树中、且到 vv 的距离恰好为 hh 的房屋。该集合称为一个公会(guild)。例如,当 h=0h = 0 时,该公会仅包含顶点 vv 自身。

下图展示了顶点 v=4v = 4 对应的各个公会。其中 h=0h = 0 的公会以红色标出,h=1h = 1 的公会以浅蓝色标出,h=2h = 2 的公会以绿色标出。

若存在某座房屋属于其中一个公会但不属于另一个,则称这两个公会不同。史蒂夫想知道:这棵树中一共有多少个互不相同且非空的公会?请帮他解决这个问题。

∗^{\text{∗}} 有根树是指一棵树中指定一个特殊顶点作为根节点。

†^{\text{†}} 树是无环的连通图。

‡^{\text{‡}} 顶点 vv 的子树是指由 vv、vv 的所有后代,以及它们之间所有边所构成的子图。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of houses in the village.

The second line contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \ldots, p_n (1≤pi<i1 \le p_i \lt i), where pip_i is the parent of the ii-th house in the tree.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 表示村庄中房屋的数量。

第二行包含 n−1n-1 个整数 p2,p3,…,pnp_2, p_3, \ldots, p_n(1≤pi<i1 \le p_i \lt i),其中 pip_i 表示树中第 ii 个房屋的父节点。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the number of different guilds in the village.

对于每个测试用例,输出一个整数——村庄中不同公会的数量。

输入输出样例

  • 输入#1

    5
    5
    1 2 3 4
    3
    1 1
    7
    1 2 1 3 5 5
    10
    1 1 3 2 2 4 4 4 3
    15
    1 2 1 3 3 4 3 7 3 10 6 7 1 9

    输出#1

    5
    4
    9
    15
    22

说明/提示

In the first testcase, there are 55 guilds, each consisting of a single house.

In the second testcase, in addition to the guilds consisting of a single house, there is also a guild consisting of vertices 22 and 33. It can be obtained by considering houses at distance h=1h=1 in the subtree of vertex v=1v=1.

In the third testcase, in addition to the guilds consisting of a single house, there are 22 more guilds: for v=1v=1 and h=1h=1, the guild consists of vertices 22 and 44; for v=5v=5, h=1h=1, the guild consists of vertices 66 and 77.

在第一个测试用例中,共有 55 个公会,每个公会仅包含一座房屋。

在第二个测试用例中,除包含单座房屋的公会外,还存在一个由顶点 22 和 33 组成的公会。该公会可通过考虑顶点 v=1v=1 的子树中距离为 h=1h=1 的房屋得到。

在第三个测试用例中,除包含单座房屋的公会外,还额外存在 22 个公会:当 v=1v=1、h=1h=1 时,公会由顶点 22 和 44 组成;当 v=5v=5、h=1h=1 时,公会由顶点 66 和 77 组成。

输入解题思路,AI测评打分。不知道怎么写?

首页