CF2254G.Nightcrawler

提高+/省选-

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

Yousef has given you a rooted tree∗^{\text{∗}} with nn vertices, where the root is vertex 11. Each vertex ii is assigned an integer aia_i.

You must partition the set of all nn vertices into exactly kk disjoint subsets S1,S2,…,SkS_1, S_2, \dots, S_k (that is, each vertex must belong to exactly one of the kk sets) such that the following condition is satisfied:

  • For any subset SiS_i containing two or more vertices, for every pair of vertices u,v∈Siu, v \in S_i, one must be the ancestor†^{\text{†}} of the other (i.e., they must all lie on the same path extending from the root toward a leaf).

The score of a subset SiS_i is defined as the maximum value aua_u among all vertices uu in that subset. The score of the partition is the sum of the scores of the kk subsets. In other words, the score of the partition is equal to ∑i=1kmax⁡u∈Siau\sum\limits_{i=1}^{k} \max\limits_{u \in S_i} a_u.

For every integer kk from 11 to nn, calculate the maximum possible score of a partition. If it is impossible to partition the tree into exactly kk subsets that satisfy the condition, output −1-1.

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

†^{\text{†}}An ancestor of vertex vv is any vertex on the simple path from vv to the root, including the root, but not including vv. The root has no ancestors.

优素福给你一棵有 nn 个顶点的有根树∗^{\text{∗}},其中根节点为顶点 11。每个顶点 ii 被赋予一个整数 aia_i。

你需要将全部 nn 个顶点划分为恰好 kk 个互不相交的子集 S1,S2,…,SkS_1, S_2, \dots, S_k(即每个顶点必须且仅属于这 kk 个子集中的一个),使得满足以下条件:

  • 对于任意包含两个或更多顶点的子集 SiS_i,对其中任意一对顶点 u,v∈Siu, v \in S_i,uu 和 vv 中必有一个是另一个的祖先†^{\text{†}}(即它们必须全部位于从根节点通向某个叶节点的同一条路径上)。

子集 SiS_i 的得分定义为该子集中所有顶点 uu 的 aua_u 的最大值。划分的得分为这 kk 个子集得分之和。换言之,划分的得分等于 ∑i=1kmax⁡u∈Siau\sum\limits_{i=1}^{k} \max\limits_{u \in S_i} a_u。

对每个从 11 到 nn 的整数 kk,计算满足条件的划分所能达到的最大得分。若无法将该树恰好划分为 kk 个满足条件的子集,则输出 −1-1。

∗^{\text{∗}}树是无环的连通图。有根树是一棵指定某一顶点为根的树。

†^{\text{†}}顶点 vv 的祖先是指从 vv 到根节点的简单路径上的任意顶点(包括根节点,但不包括 vv 自身)。根节点没有祖先。

输入格式

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains an integer nn (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5) — the number of vertices.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the values of the vertices.

The third line of each test case contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \dots, p_n (1≤pi<i1 \le p_i \lt i), where pip_i is the parent of the ii-th vertex.

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(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)—— 顶点的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 各顶点的值。

每个测试用例的第三行包含 n−1n-1 个整数 p2,p3,…,pnp_2, p_3, \dots, 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 line containing nn space-separated integers. The kk-th integer should represent the maximum possible score for a partition of kk subsets. If it is impossible to partition the tree into kk subsets, output −1-1 for that value.

对于每个测试用例,输出一行,包含 nn 个以空格分隔的整数。其中第 kk 个整数应表示将树划分为 kk 个子集时所能达到的最大得分。若无法将树划分为 kk 个子集,则对应位置输出 −1-1。

输入输出样例

  • 输入#1

    7
    3
    10 20 30
    1 1
    4
    5 10 15 20
    1 2 2
    4
    1 2 3 4
    1 1 3
    5
    1 2 3 4 5
    1 2 3 4
    9
    1 100 1 90 80 1 2 3 4
    1 1 2 4 3 3 3 3
    6
    5 4 10 3 9 1
    1 2 1 4 1
    4
    10 10 20 1
    1 2 1

    输出#1

    -1 50 60
    -1 35 45 50
    -1 6 9 10
    5 9 12 14 15
    -1 -1 -1 -1 110 200 280 281 282
    -1 -1 24 28 31 32
    -1 30 40 41

说明/提示

In the first test case:

  • For k=1k = 1, we would need all the vertices to be in the same set. However, for vertices 22 and 33, neither of them is the ancestor of the other. Therefore, there is no valid partition.
  • For k=2k = 2, we can make S1=2S_1 = {2}, S2=1,3S_2 = {1, 3}. The score of this partition is max⁡u∈S1au+max⁡u∈S2au=20+30=50\max\limits_{u \in S_1} a_u + \max\limits_{u \in S_2} a_u = 20 + 30 = 50. It can be shown that this is the maximum score.
  • For k=3k = 3, we can make S1=1S_1 = {1}, S2=2S_2 = {2}, S3=3S_3 = {3}. The score of this partition is 10+20+30=6010 + 20 + 30 = 60.

The given tree in the first test case.

In the second test case:

  • For k=1k = 1, we would need all the vertices to be in the same set. However, vertices 33 and 44 are not on the same root-to-leaf path, so this is impossible.
  • For k=2k = 2, we can make S1=3S_1 = {3}, S2=1,2,4S_2 = {1,2,4}. The score of this partition is max⁡u∈S1au+max⁡u∈S2au=15+20=35\max\limits_{u \in S_1} a_u + \max\limits_{u \in S_2} a_u = 15 + 20 = 35. It can be shown that this is the maximum score.
  • For k=3k = 3, we can make S1=3S_1 = {3}, S2=4S_2 = {4}, S3=1,2S_3 = {1,2}. The score of this partition is 15+20+10=4515 + 20 + 10 = 45. It can be shown that this is the maximum score.
  • For k=4k = 4, we can make S1=1S_1 = {1}, S2=2S_2 = {2}, S3=3S_3 = {3}, S4=4S_4 = {4}. The score of this partition is 5+10+15+20=505 + 10 + 15 + 20 = 50.

The given tree in the second test case.

在第一个测试用例中:

  • 当 k=1k = 1 时,我们需要将所有顶点放入同一个集合中。然而,对于顶点 22 和 33,它们互不为对方的祖先。因此,不存在合法的划分。
  • 当 k=2k = 2 时,我们可以令 S1={2}S_1 = \{2\},S2={1,3}S_2 = \{1, 3\}。该划分的得分为 max⁡u∈S1au+max⁡u∈S2au=20+30=50\max\limits_{u \in S_1} a_u + \max\limits_{u \in S_2} a_u = 20 + 30 = 50。可以证明这是最大得分。
  • 当 k=3k = 3 时,我们可以令 S1={1}S_1 = \{1\},S2={2}S_2 = \{2\},S3={3}S_3 = \{3\}。该划分的得分为 10+20+30=6010 + 20 + 30 = 60。

第一个测试用例中给出的树。

在第二个测试用例中:

  • 当 k=1k = 1 时,我们需要将所有顶点放入同一个集合中。然而,顶点 33 和 44 不在同一条根到叶的路径上,因此这是不可能的。
  • 当 k=2k = 2 时,我们可以令 S1={3}S_1 = \{3\},S2={1,2,4}S_2 = \{1,2,4\}。该划分的得分为 max⁡u∈S1au+max⁡u∈S2au=15+20=35\max\limits_{u \in S_1} a_u + \max\limits_{u \in S_2} a_u = 15 + 20 = 35。可以证明这是最大得分。
  • 当 k=3k = 3 时,我们可以令 S1={3}S_1 = \{3\},S2={4}S_2 = \{4\},S3={1,2}S_3 = \{1,2\}。该划分的得分为 15+20+10=4515 + 20 + 10 = 45。可以证明这是最大得分。
  • 当 k=4k = 4 时,我们可以令 S1={1}S_1 = \{1\},S2={2}S_2 = \{2\},S3={3}S_3 = \{3\},S4={4}S_4 = \{4\}。该划分的得分为 5+10+15+20=505 + 10 + 15 + 20 = 50。

第二个测试用例中给出的树。

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

首页