CF1485E.Move and Swap

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given n−1n - 1 integers a2,…,ana_2, \dots, a_n and a tree with nn vertices rooted at vertex 11. The leaves are all at the same distance dd from the root.

Recall that a tree is a connected undirected graph without cycles. The distance between two vertices is the number of edges on the simple path between them. All non-root vertices with degree 11 are leaves. If vertices ss and ff are connected by an edge and the distance of ff from the root is greater than the distance of ss from the root, then ff is called a child of ss.

Initially, there are a red coin and a blue coin on the vertex 11. Let rr be the vertex where the red coin is and let bb be the vertex where the blue coin is. You should make dd moves. A move consists of three steps:

  • Move the red coin to any child of rr.
  • Move the blue coin to any vertex b′b' such that dist(1,b′)=dist(1,b)+1dist(1, b') = dist(1, b) + 1. Here dist(x,y)dist(x, y) indicates the length of the simple path between xx and yy. Note that bb and b′b' are not necessarily connected by an edge.
  • You can optionally swap the two coins (or skip this step).

Note that rr and bb can be equal at any time, and there is no number written on the root.

After each move, you gain ∣ar−ab∣|a_r - a_b| points. What's the maximum number of points you can gain after dd moves?

给你 n−1n - 1 个整数 a2,…,ana_2, \dots, a_n 和一棵以顶点 11 为根、含 nn 个顶点的树。所有叶子节点到根节点的距离均为 dd。

回忆一下:树是一种无环的连通无向图。两个顶点之间的距离定义为它们之间唯一简单路径上的边数。所有非根且度数为 11 的顶点均为叶子节点。若顶点 ss 与 ff 由一条边相连,且 ff 到根的距离大于 ss 到根的距离,则称 ff 是 ss 的一个子节点。

初始时,一枚红币和一枚蓝币均位于顶点 11 上。设 rr 表示红币所在顶点,bb 表示蓝币所在顶点。你需要恰好执行 dd 次移动。每次移动包含以下三个步骤:

  • 将红币移动至 rr 的任意一个子节点;
  • 将蓝币移动至任意一个顶点 b′b',使得 dist(1,b′)=dist(1,b)+1dist(1, b') = dist(1, b) + 1。其中 dist(x,y)dist(x, y) 表示 xx 与 yy 之间简单路径的长度。注意:bb 与 b′b' 不一定由边直接相连;
  • 你可以选择交换两枚硬币的位置(也可跳过此步)。

注意:在任意时刻,rr 和 bb 可以重合;且根节点上没有数字。

每次移动后,你将获得 ∣ar−ab∣|a_r - a_b| 分。问:经过 dd 次移动后,最多能获得多少分?

输入格式

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

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

The second line of each test case contains n−1n-1 integers v2,v3,…,vnv_2, v_3, \dots, v_n (1≤vi≤n1 \leq v_i \leq n, vi≠iv_i \neq i) — the ii-th of them indicates that there is an edge between vertices ii and viv_i. It is guaranteed, that these edges form a tree.

The third line of each test case contains n−1n-1 integers a2,…,ana_2, \dots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — the numbers written on the vertices.

It is guaranteed that the sum of nn for 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 \leq n \leq 2 \cdot 10^5)—— 表示树中顶点的数量。

每个测试用例的第二行包含 n−1n-1 个整数 v2,v3,…,vnv_2, v_3, \dots, v_n(1≤vi≤n1 \leq v_i \leq n,且 vi≠iv_i \neq i)—— 其中第 ii 个数表示顶点 ii 与顶点 viv_i 之间存在一条边。保证这些边构成一棵树。

每个测试用例的第三行包含 n−1n-1 个整数 a2,…,ana_2, \dots, a_n(1≤ai≤1091 \leq a_i \leq 10^9)—— 表示写在各顶点上的数字。

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

输出格式

For each test case, print a single integer: the maximum number of points you can gain after dd moves.

对于每个测试用例,输出一个整数:经过 dd 步移动后你能获得的最大点数。

输入输出样例

  • 输入#1

    4
    14
    1 1 1 2 3 4 4 5 5 6 7 8 8
    2 3 7 7 6 9 5 9 7 3 6 6 5
    6
    1 2 2 3 4
    32 78 69 5 41
    15
    1 15 1 10 4 9 11 2 4 1 8 6 10 11
    62 13 12 43 39 65 42 86 25 38 19 19 43 62
    15
    11 2 7 6 9 8 10 1 1 1 5 3 15 2
    50 19 30 35 9 45 13 24 8 44 16 26 10 40

    输出#1

    14
    45
    163
    123

说明/提示

In the first test case, an optimal solution is to:

  • move 11: r=4r = 4, b=2b = 2; no swap;
  • move 22: r=7r = 7, b=6b = 6; swap (after it r=6r = 6, b=7b = 7);
  • move 33: r=11r = 11, b=9b = 9; no swap.

The total number of points is ∣7−2∣+∣6−9∣+∣3−9∣=14|7 - 2| + |6 - 9| + |3 - 9| = 14.

In the second test case, an optimal solution is to:

  • move 11: r=2r = 2, b=2b = 2; no swap;
  • move 22: r=3r = 3, b=4b = 4; no swap;
  • move 33: r=5r = 5, b=6b = 6; no swap.

The total number of points is ∣32−32∣+∣78−69∣+∣5−41∣=45|32 - 32| + |78 - 69| + |5 - 41| = 45.

在第一个测试用例中,一个最优解为:

  • 第 1 步:r=4r = 4,b=2b = 2;不交换;
  • 第 2 步:r=7r = 7,b=6b = 6;交换(交换后 r=6r = 6,b=7b = 7);
  • 第 3 步:r=11r = 11,b=9b = 9;不交换。

总得分为 ∣7−2∣+∣6−9∣+∣3−9∣=14|7 - 2| + |6 - 9| + |3 - 9| = 14。

在第二个测试用例中,一个最优解为:

  • 第 1 步:r=2r = 2,b=2b = 2;不交换;
  • 第 2 步:r=3r = 3,b=4b = 4;不交换;
  • 第 3 步:r=5r = 5,b=6b = 6;不交换。

总得分为 ∣32−32∣+∣78−69∣+∣5−41∣=45|32 - 32| + |78 - 69| + |5 - 41| = 45。

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

首页