CF1916E.Happy Life in University

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Egor and his friend Arseniy are finishing school this year and will soon enter university. And since they are very responsible guys, they have started preparing for admission already.

First of all, they decided to take care of where they will live for the long four years of study, and after visiting the university's website, they found out that the university dormitory can be represented as a root tree with nn vertices with the root at vertex 11. In the tree, each vertex represents a recreation with some type of activity aia_i. The friends need to choose 22 recreations (not necessarily different) in which they will settle. The guys are convinced that the more the value of the following function f(u,v)=diff(u,lca(u,v))⋅diff(v,lca(u,v))f(u, v) = diff(u, lca(u, v)) \cdot diff(v, lca(u, v)), the more fun their life will be. Help Egor and Arseniy and find the maximum value of f(u,v)f(u, v) among all pairs of recreations!

†diff(u,v)^{\dagger} diff(u, v) — the number of different activities listed on the simple path from vertex uu to vertex vv.

†lca(u,v)^{\dagger} lca(u, v) — a vertex pp such that it is at the maximum distance from the root and is a parent of both vertex uu and vertex vv.

伊戈尔和他的朋友阿尔谢尼今年即将高中毕业,并很快就要进入大学。由于他们是非常有责任心的人,因此早已开始为升学做准备。

首先,他们决定先解决未来四年学习期间的住宿问题。在浏览了大学官网后,他们发现大学宿舍可以表示为一棵以顶点 11 为根的、包含 nn 个顶点的有根树。在这棵树中,每个顶点代表一个活动室,其活动类型为 aia_i。两位朋友需要从中选择 22 个活动室(可以相同)作为他们的住所。他们坚信:函数 f(u,v)=diff(u,lca(u,v))⋅diff(v,lca(u,v))f(u, v) = diff(u, lca(u, v)) \cdot diff(v, lca(u, v)) 的值越大,他们的大学生活就越有趣。请帮助伊戈尔和阿尔谢尼,找出所有活动室对 (u,v)(u, v) 中 f(u,v)f(u, v) 的最大值!

†diff(u,v)^{\dagger} diff(u, v) —— 表示从顶点 uu 到顶点 vv 的简单路径上所经过的不同活动类型的数量。

†lca(u,v)^{\dagger} lca(u, v) —— 指满足以下条件的顶点 pp:pp 是 uu 和 vv 的公共祖先,且在所有公共祖先中距离根节点最远。

输入格式

Each test consists of several test cases. The first line contains a single integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer nn (1≤n≤3⋅1051 \le n \le 3 \cdot 10^{5}).

The second line of each test case contains n−1{n - 1} integers p2,p3,…,pnp_2, p_3, \ldots,p_n (1≤pi≤i−11 \le p_i \le i - 1), where pip_i — the parent of vertex ii.

The third line of each test case contains n{n} integers a1,a2,…,ana_1, a_2, \ldots,a_n (1≤ai≤n1 \le a_i \le n), where aia_i — the number of the activity located at vertex ii.

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

每个测试包含若干测试用例。第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤3⋅1051 \le n \le 3 \cdot 10^{5})。

每个测试用例的第二行包含 n−1n - 1 个整数 p2,p3,…,pnp_2, p_3, \ldots,p_n(1≤pi≤i−11 \le p_i \le i - 1),其中 pip_i 表示顶点 ii 的父节点。

每个测试用例的第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots,a_n(1≤ai≤n1 \le a_i \le n),其中 aia_i 表示位于顶点 ii 处的活动编号。

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

输出格式

For each test case, output the maximum value of f(u,v)f(u, v) for all pairs of recreations (u,v)(u, v).

对于每个测试用例,输出所有娱乐活动对 (u,v)(u, v) 对应的 f(u,v)f(u, v) 的最大值。

输入输出样例

  • 输入#1

    4
    2
    1
    1 2
    7
    1 1 2 2 3 3
    6 5 2 3 6 5 6
    13
    1 1 1 2 2 2 3 3 4 5 6 6
    2 2 2 1 4 9 7 2 5 2 1 11 2
    12
    1 1 1 2 2 3 4 4 7 7 6
    11 2 1 11 12 8 5 8 8 5 11 7

    输出#1

    2
    9
    9
    12

说明/提示

Consider the fourth test case. The tree has the following structure:

All recreations are colored. The same colors mean that the activities in the recreations match. Consider the pair of vertices (11,12)(11, 12), lca(11,12)=1lca(11, 12) = 1. Write down all activities on the path from 1111 to 11 — [11,5,1,11][11, 5, 1, 11], among them there are 33 different activities, so diff(11,1)=3diff(11, 1) = 3. Also write down all activities on the path from 1212 to 11 — [7,8,2,11][7, 8, 2, 11], among them there are 44 different activities, so diff(12,1)=4diff(12, 1) = 4. We get that f(11,12)=diff(12,1)⋅diff(11,1)=4⋅3=12f(11, 12) = diff(12, 1) \cdot diff(11, 1) = 4 \cdot 3 = 12, which is the answer for this tree. It can be shown that a better answer is impossible to obtain.

考虑第四个测试用例。该树具有如下结构:

所有休闲活动均被染色,相同颜色表示对应休闲活动相同。考虑顶点对 (11,12)(11, 12),其最近公共祖先为 lca(11,12)=1lca(11, 12) = 1。写出从 1111 到 11 的路径上所有活动:[11,5,1,11][11, 5, 1, 11],其中包含 33 种不同的活动,因此 diff(11,1)=3diff(11, 1) = 3;同样,写出从 1212 到 11 的路径上所有活动:[7,8,2,11][7, 8, 2, 11],其中包含 44 种不同的活动,因此 diff(12,1)=4diff(12, 1) = 4。于是得到 f(11,12)=diff(12,1)⋅diff(11,1)=4⋅3=12f(11, 12) = diff(12, 1) \cdot diff(11, 1) = 4 \cdot 3 = 12,即为该树的答案。可以证明,无法得到更优的答案。

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

首页