CF1778F.Maximizing Root

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given a rooted tree consisting of nn vertices numbered from 11 to nn. Vertex 11 is the root of the tree. Each vertex has an integer value. The value of ii-th vertex is aia_i. You can do the following operation at most kk times.

  • Choose a vertex vv that has not been chosen before and an integer xx such that xx is a common divisor of the values of all vertices of the subtree of vv. Multiply by xx the value of each vertex in the subtree of vv.

What is the maximum possible value of the root node 11 after at most kk operations? Formally, you have to maximize the value of a1a_1.

A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. The subtree of a node uu is the set of all nodes yy such that the simple path from yy to the root passes through uu. Note that uu is in the subtree of uu.

你被给定一棵包含 nn 个顶点的有根树,顶点编号从 11 到 nn。顶点 11 是该树的根。每个顶点都有一个整数值,其中第 ii 个顶点的值为 aia_i。你最多可以执行以下操作 kk 次:

  • 选择一个此前未被选过的顶点 vv,以及一个整数 xx,使得 xx 是 vv 的子树中所有顶点的值的一个公因数;然后将 vv 的子树中每个顶点的值都乘以 xx。

在最多执行 kk 次操作后,根节点 11 的值 a1a_1 的最大可能值是多少?形式上,你需要最大化 a1a_1 的值。

树是一种无环的连通无向图。有根树是一棵选定某个顶点作为根的树。节点 uu 的子树是指所有满足“从 yy 到根的简单路径经过 uu”的节点 yy 所构成的集合。注意:uu 自身属于其子树。

输入格式

The first line contains an integer tt (1≤t≤50 0001 \leq t \leq 50\,000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and kk (2≤n≤1052 \leq n \leq 10^5, 0≤k≤n0 \leq k \leq n) — the number of vertices in the tree and the number of operations.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤10001 \leq a_i \leq 1000), where aia_i denotes the value of vertex ii.

Each of the next n−1n - 1 lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \neq v_i), denoting the edge of the tree between vertices uiu_i and viv_i. 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≤50 0001 \leq t \leq 50\,000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(2≤n≤1052 \leq n \leq 10^5,0≤k≤n0 \leq k \leq n),分别表示树中顶点的数量和操作次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤10001 \leq a_i \leq 1000),其中 aia_i 表示顶点 ii 的权值。

接下来的 n−1n - 1 行中,每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,ui≠viu_i \neq v_i),表示树中连接顶点 uiu_i 和 viv_i 的一条边。保证所给边构成一棵树。

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

输出格式

For each test case, output the maximum value of the root after performing at most kk operations.

对于每个测试用例,输出执行至多 kk 次操作后根节点的最大值。

输入输出样例

  • 输入#1

    2
    5 2
    24 12 24 6 12
    1 2
    1 3
    2 4
    2 5
    5 3
    24 12 24 6 12
    1 2
    1 3
    2 4
    2 5

    输出#1

    288
    576

说明/提示

Both examples have the same tree:

For the first test case, you can do two operations as follows:

  • Choose the subtree of vertex 44 and x=2x = 2.

    After this operation, the node values become 24,12,24,12,12.{24, 12, 24, 12, 12}.

  • Choose the subtree of vertex 11 and x=12x = 12.

    After this operation, the node values become 288,144,288,144,144.{288, 144, 288, 144, 144}.

The value of the root is 288288 and it is the maximum.

For the second test case, you can do three operations as follows:

  • Choose the subtree of vertex 44 and x=2x = 2.

    After this operation, the node values become 24,12,24,12,12.{24, 12, 24, 12, 12}.

  • Choose the subtree of vertex 22 and x=4x = 4.

    After this operation, the node values become 24,48,24,48,48.{24, 48, 24, 48, 48}.

  • Choose the subtree of vertex 11 and x=24x = 24.

    After this operation, the node values become 576,1152,576,1152,1152.{576, 1152, 576, 1152, 1152}.

The value of the root is 576576 and it is the maximum.

两个示例具有相同的树:

对于第一个测试用例,你可以执行如下两次操作:

  • 选择顶点 44 的子树,并取 x=2x = 2。

    此操作后,各节点的值变为 24,12,24,12,12{24, 12, 24, 12, 12}。

  • 选择顶点 11 的子树,并取 x=12x = 12。

    此操作后,各节点的值变为 288,144,288,144,144{288, 144, 288, 144, 144}。

根节点的值为 288288,且这是最大值。

对于第二个测试用例,你可以执行如下三次操作:

  • 选择顶点 44 的子树,并取 x=2x = 2。

    此操作后,各节点的值变为 24,12,24,12,12{24, 12, 24, 12, 12}。

  • 选择顶点 22 的子树,并取 x=4x = 4。

    此操作后,各节点的值变为 24,48,24,48,48{24, 48, 24, 48, 48}。

  • 选择顶点 11 的子树,并取 x=24x = 24。

    此操作后,各节点的值变为 576,1152,576,1152,1152{576, 1152, 576, 1152, 1152}。

根节点的值为 576576,且这是最大值。

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

首页