CF2266G.Modular Tree

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Vihaan has a rooted tree∗^{\text{∗}} with nn nodes. The tree is rooted at node 11.

Each node ii has an initial value aia_i and a modulus bib_i. Let xix_i denote the current value of node ii. Initially, xi=aix_i=a_i.

Vihaan may perform the following operation any number of times:

  • Choose a node uu. Let ss be the sum of the current values of all direct children of uu, then replace xux_u with (xu+s) mod bu(x_u+s)\bmod b_u.

After performing any number of operations, Vihaan wants to maximize the sum of the values of all nodes.

Determine the maximum possible sum.

∗^{\text{∗}}A tree is an undirected connected graph in which there are no cycles.

维汉有一棵含 nn 个节点的有根树∗^{\text{∗}},树根为节点 11。

每个节点 ii 有一个初始值 aia_i 和一个模数 bib_i。记节点 ii 的当前值为 xix_i,初始时 xi=aix_i = a_i。

维汉可以任意次执行以下操作:

  • 选择一个节点 uu。令 ss 为 uu 的所有直接子节点的当前值之和,然后将 xux_u 替换为 (xu+s) mod bu(x_u + s) \bmod b_u。

在执行任意次数的操作后,维汉希望最大化所有节点的值之和。

请确定该和的最大可能值。

∗^{\text{∗}} 树是一种无向连通图,其中不存在环。

输入格式

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 (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of nodes in the tree.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (0≤ai<bi0 \le a_i \lt b_i) — the initial values of the nodes.

The third line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n (1≤bi≤1091 \le b_i \le 10^9) — the moduli of the nodes.

Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1 \le u,v \le n) — an edge between nodes uu and vv.

It is guaranteed that the given edges form a 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(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 树中节点的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai<bi0 \le a_i \lt b_i)—— 各节点的初始值。

第三行包含 nn 个整数 b1,b2,…,bnb_1,b_2,\ldots,b_n(1≤bi≤1091 \le b_i \le 10^9)—— 各节点的模数。

接下来的 n−1n-1 行中,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u,v \le n)—— 表示节点 uu 与节点 vv 之间的一条边。

保证所给的边构成一棵树。

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

输出格式

For each test case, print one integer — the maximum possible sum of the values of all nodes after performing any number of operations.

对于每个测试用例,输出一个整数——执行任意次数操作后,所有节点的值之和的最大可能值。

输入输出样例

  • 输入#1

    8
    1
    3
    7
    2
    0 3
    5 4
    1 2
    3
    0 2 3
    7 3 4
    1 2
    2 3
    3
    0 0 1
    5 2 2
    1 2
    2 3
    4
    1 2 3 4
    10 3 4 5
    1 2
    1 3
    1 4
    3
    0 1 3
    10 2 4
    1 2
    1 3
    5
    0 0 1 2 3
    12 6 9 3 4
    1 2
    1 3
    2 4
    3 5
    4
    0 999999999 999999999 999999999
    1000000000 1000000000 1000000000 1000000000
    1 2
    1 3
    1 4

    输出#1

    3
    7
    11
    6
    18
    12
    27
    3999999996

说明/提示

In the first test case, node 11 has no children, so performing an operation on it does not change its value. Thus, the maximum possible sum is 33.

In the third test case, Vihaan can perform the operation on node 11 three times:

  1. [0,2,3]→[2,2,3][0,2,3] \to [2,2,3].
  2. [2,2,3]→[4,2,3][2,2,3] \to [4,2,3].
  3. [4,2,3]→[6,2,3][4,2,3] \to [6,2,3].

The resulting sum is 6+2+3=116+2+3=11. It can be shown that no sequence of operations can obtain a larger sum.

In the fourth test case, Vihaan can perform the following operations:

  1. Perform the operation on node 22: [0,0,1]→[0,1,1].[0,0,1] \to [0,1,1].
  2. Perform the operation on node 11 four times: [0,1,1]→[1,1,1]→[2,1,1]→[3,1,1]→[4,1,1].[0,1,1] \to [1,1,1] \to [2,1,1] \to [3,1,1] \to [4,1,1].

The resulting sum is 4+1+1=64+1+1=6. It can be shown that no sequence of operations can obtain a larger sum.

在第一个测试用例中,节点 11 没有子节点,因此对其执行操作不会改变其值。于是,可能的最大和为 33。

在第三个测试用例中,Vihaan 可以对节点 11 执行三次操作:

  1. [0,2,3]→[2,2,3][0,2,3] \to [2,2,3]。
  2. [2,2,3]→[4,2,3][2,2,3] \to [4,2,3]。
  3. [4,2,3]→[6,2,3][4,2,3] \to [6,2,3]。

最终得到的和为 6+2+3=116+2+3=11。可以证明,不存在任何操作序列能得到更大的和。

在第四个测试用例中,Vihaan 可以执行以下操作:

  1. 对节点 22 执行操作:[0,0,1]→[0,1,1][0,0,1] \to [0,1,1]。
  2. 对节点 11 执行四次操作:[0,1,1]→[1,1,1]→[2,1,1]→[3,1,1]→[4,1,1][0,1,1] \to [1,1,1] \to [2,1,1] \to [3,1,1] \to [4,1,1]。

最终得到的和为 4+1+1=64+1+1=6。可以证明,不存在任何操作序列能得到更大的和。

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

首页