CF2266G.Modular Tree
提高+/省选-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Vihaan has a rooted tree∗ with n nodes. The tree is rooted at node 1.
Each node i has an initial value ai and a modulus bi. Let xi denote the current value of node i. Initially, xi=ai.
Vihaan may perform the following operation any number of times:
- Choose a node u. Let s be the sum of the current values of all direct children of u, then replace xu with (xu+s)modbu.
After performing any number of operations, Vihaan wants to maximize the sum of the values of all nodes.
Determine the maximum possible sum.
∗A tree is an undirected connected graph in which there are no cycles.
维汉有一棵含 n 个节点的有根树∗,树根为节点 1。
每个节点 i 有一个初始值 ai 和一个模数 bi。记节点 i 的当前值为 xi,初始时 xi=ai。
维汉可以任意次执行以下操作:
- 选择一个节点 u。令 s 为 u 的所有直接子节点的当前值之和,然后将 xu 替换为 (xu+s)modbu。
在执行任意次数的操作后,维汉希望最大化所有节点的值之和。
请确定该和的最大可能值。
∗ 树是一种无向连通图,其中不存在环。
输入格式
The first line contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the number of nodes in the tree.
The second line contains n integers a1,a2,…,an (0≤ai<bi) — the initial values of the nodes.
The third line contains n integers b1,b2,…,bn (1≤bi≤109) — the moduli of the nodes.
Each of the next n−1 lines contains two integers u and v (1≤u,v≤n) — an edge between nodes u and v.
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 树中节点的数量。
第二行包含 n 个整数 a1,a2,…,an(0≤ai<bi)—— 各节点的初始值。
第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤109)—— 各节点的模数。
接下来的 n−1 行中,每行包含两个整数 u 和 v(1≤u,v≤n)—— 表示节点 u 与节点 v 之间的一条边。
保证所给的边构成一棵树。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
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 1 has no children, so performing an operation on it does not change its value. Thus, the maximum possible sum is 3.
In the third test case, Vihaan can perform the operation on node 1 three times:
- [0,2,3]→[2,2,3].
- [2,2,3]→[4,2,3].
- [4,2,3]→[6,2,3].
The resulting sum is 6+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:
- Perform the operation on node 2: [0,0,1]→[0,1,1].
- Perform the operation on node 1 four times: [0,1,1]→[1,1,1]→[2,1,1]→[3,1,1]→[4,1,1].
The resulting sum is 4+1+1=6. It can be shown that no sequence of operations can obtain a larger sum.
在第一个测试用例中,节点 1 没有子节点,因此对其执行操作不会改变其值。于是,可能的最大和为 3。
在第三个测试用例中,Vihaan 可以对节点 1 执行三次操作:
- [0,2,3]→[2,2,3]。
- [2,2,3]→[4,2,3]。
- [4,2,3]→[6,2,3]。
最终得到的和为 6+2+3=11。可以证明,不存在任何操作序列能得到更大的和。
在第四个测试用例中,Vihaan 可以执行以下操作:
- 对节点 2 执行操作:[0,0,1]→[0,1,1]。
- 对节点 1 执行四次操作:[0,1,1]→[1,1,1]→[2,1,1]→[3,1,1]→[4,1,1]。
最终得到的和为 4+1+1=6。可以证明,不存在任何操作序列能得到更大的和。
输入解题思路,AI测评打分。不知道怎么写?