CF1746D.Paths on the Tree

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a rooted tree consisting of nn vertices. The vertices are numbered from 11 to nn, and the root is the vertex 11. You are also given a score array s1,s2,…,sns_1, s_2, \ldots, s_n.

A multiset of kk simple paths is called valid if the following two conditions are both true.

  • Each path starts from 11.
  • Let cic_i be the number of paths covering vertex ii. For each pair of vertices (u,v)(u,v) (2≤u,v≤n2\le u,v\le n) that have the same parent, ∣cu−cv∣≤1|c_u-c_v|\le 1 holds.

The value of the path multiset is defined as ∑i=1ncisi\sum\limits_{i=1}^n c_i s_i.

It can be shown that it is always possible to find at least one valid multiset. Find the maximum value among all valid multisets.

你被给定一棵包含 nn 个顶点的有根树。顶点编号为 11 到 nn,根节点为顶点 11。同时,你还被给定一个分数数组 s1,s2,…,sns_1, s_2, \ldots, s_n。

一个包含 kk 条简单路径的多重集被称为合法的,当且仅当以下两个条件均成立:

  • 每条路径均从顶点 11 出发;
  • 设 cic_i 表示覆盖顶点 ii 的路径条数。对任意一对具有相同父节点的顶点 (u,v)(u,v)(其中 2≤u,v≤n2\le u,v\le n),均有 ∣cu−cv∣≤1|c_u-c_v|\le 1。

该路径多重集的值定义为 ∑i=1ncisi\sum\limits_{i=1}^n c_i s_i。

可以证明,总存在至少一个合法的多重集。请找出所有合法多重集中最大的值。

输入格式

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

The first line of each test case contains two space-separated integers nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) and kk (1≤k≤1091 \le k \le 10^9) — the size of the tree and the required number of paths.

The second line contains n−1n - 1 space-separated integers p2,p3,…,pnp_2,p_3,\ldots,p_n (1≤pi≤n1\le p_i\le n), where pip_i is the parent of the ii-th vertex. It is guaranteed that this value describe a valid tree with root 11.

The third line contains nn space-separated integers s1,s2,…,sns_1,s_2,\ldots,s_n (0≤si≤1040 \le s_i \le 10^4) — the scores of the vertices.

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

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

每个测试用例的第一行包含两个以空格分隔的整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)和 kk(1≤k≤1091 \le k \le 10^9),分别表示树的大小以及所需路径的数量。

第二行包含 n−1n - 1 个以空格分隔的整数 p2,p3,…,pnp_2,p_3,\ldots,p_n(1≤pi≤n1\le p_i\le n),其中 pip_i 表示第 ii 个顶点的父节点。保证这些值构成一棵以 11 为根的合法树。

第三行包含 nn 个以空格分隔的整数 s1,s2,…,sns_1,s_2,\ldots,s_n(0≤si≤1040 \le s_i \le 10^4),表示各顶点的分数。

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

输出格式

For each test case, print a single integer — the maximum value of a path multiset.

对于每个测试用例,输出一个整数——路径多重集的最大值。

输入输出样例

  • 输入#1

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

    输出#1

    54
    56

说明/提示

In the first test case, one of optimal solutions is four paths 1→2→3→51 \to 2 \to 3 \to 5, 1→2→3→51 \to 2 \to 3 \to 5, 1→41 \to 4, 1→41 \to 4, here c=[4,2,2,2,2]c=[4,2,2,2,2]. The value equals to 4⋅6+2⋅2+2⋅1+2⋅5+2⋅7=544\cdot 6+ 2\cdot 2+2\cdot 1+2\cdot 5+2\cdot 7=54.

In the second test case, one of optimal solution is three paths 1→2→3→51 \to 2 \to 3 \to 5, 1→2→3→51 \to 2 \to 3 \to 5, 1→41 \to 4, here c=[3,2,2,1,2]c=[3,2,2,1,2]. The value equals to 3⋅6+2⋅6+2⋅1+1⋅4+2⋅10=563\cdot 6+ 2\cdot 6+2\cdot 1+1\cdot 4+2\cdot 10=56.

在第一个测试用例中,一种最优解包含四条路径:1→2→3→51 \to 2 \to 3 \to 5、1→2→3→51 \to 2 \to 3 \to 5、1→41 \to 4、1→41 \to 4,此时 c=[4,2,2,2,2]c=[4,2,2,2,2]。该解的值为 4⋅6+2⋅2+2⋅1+2⋅5+2⋅7=544\cdot 6+ 2\cdot 2+2\cdot 1+2\cdot 5+2\cdot 7=54。

在第二个测试用例中,一种最优解包含三条路径:1→2→3→51 \to 2 \to 3 \to 5、1→2→3→51 \to 2 \to 3 \to 5、1→41 \to 4,此时 c=[3,2,2,1,2]c=[3,2,2,1,2]。该解的值为 3⋅6+2⋅6+2⋅1+1⋅4+2⋅10=563\cdot 6+ 2\cdot 6+2\cdot 1+1\cdot 4+2\cdot 10=56。

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

首页