#P9846. [ICPC2021 Nanjing R] Paimon's Tree

[ICPC2021 Nanjing R] Paimon's Tree

题目描述

Paimon has found a tree with (n+1)(n + 1) initially white vertices in her left pocket and decides to play with it. A tree with (n+1)(n + 1) nodes is an undirected connected graph with nn edges.

Paimon will give you an integer sequence a1,a2,,ana_1, a_2, \cdots, a_n of length nn. We first need to select a vertex in the tree and paint it black. Then we perform the following operation nn times.

During the ii-th operation, we select a white vertex xix_i which is directly connected with a black vertex yiy_i by an edge, set the weight of that edge to aia_i and also paint xix_i in black. After these nn operations we get a tree whose edges are all weighted.

What's the maximum length of the diameter of the weighted tree if we select the vertices optimally? The diameter of a weighted tree is the longest simple path in that tree. The length of a simple path is the sum of the weights of all edges in that path.

输入格式

There are multiple test cases. The first line of the input contains an integer TT (1T5×1031 \le T \le 5 \times 10^3) indicating the number of test cases. For each test case:

The first line contains an integer nn (1n1501 \le n \le 150) indicating the length of the sequence.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (1ai1091 \le a_i \le 10^9) indicating the sequence.

For the following nn lines, the ii-th line contains two integers uiu_i and viv_i (1ui,vin+11 \le u_i, v_i \le n + 1) indicating that there is an edge connecting vertex uiu_i and viv_i in the tree.

It's guaranteed that there is at most 1010 test cases satisfying n>20n > 20.

输出格式

For each test case output one line containing one integer indicating the maximum length of the diameter of the tree.

题目大意

Paimon's Tree

Translation

题目描述

派蒙在她的左口袋中找到了一颗有 (n+1)(n+1) 个白色节点的树。一颗有 (n+1)(n+1) 个节点的树是一个有 nn 条边的无向连通图。

派蒙会给你一个长度为 nn 的整数序列 a1,a2,...,ana_1,a_2,...,a_n 。我们首先需要选择这棵树中的一个节点并将它涂黑。接下来进行以下操作 nn 次。

在第 ii 次操作中,我们选择一个与一个黑色节点 yiy_i 直连的白色节点 xix_i ,将这条边的权值设为 aia_i ,并且将节点 xix_i 涂黑。

进行上述的 nn 次操作后,我们会得到一棵每条边都有权值的树。

在最优的选择节点策略下,这颗树的直径最大是多少?一棵树的直径是这棵树中的最长简单路径的长度。一条简单路径的长度是这条路径中所有边的权值之和。

输入格式

一次运行将会给出多个测试数据。输入的第一行包含一个整数 TT ,表示测试数据的组数。

对于每个测试数据:

第一行包括一个整数 nn ,表示序列 aa 的长度。

第二行包括 nn 个整数 a1,a2,...,ana_1,a_2,...,a_n ,表示序列 aa 的内容。

在接下来的 nn 行中,第 ii 行包括两个整数 uiu_iviv_i ,表示在这棵树上 uiu_iviv_i 有一条连边。

输出格式

对于每个测试数据,输出一行,表示这棵树在最优操作下的直径长度。

数据范围

  • 1T5×1031\le T\le 5\times 10^3
  • 1n1501\le n\le 150
  • 1ai1091\le a_i\le 10^9
  • 1ui,vin+11\le u_i,v_i\le n+1
  • 保证每个样例中最多有 1010 组测试数据满足 n>20n>20
2
5
1 7 3 5 4
1 3
2 3
3 4
4 5
4 6
1
1000000000
1 2

16
1000000000

提示

For the first sample test case, we select the vertices in the order of 1,3,4,5,2,61, 3, 4, 5, 2, 6, resulting in the weighted tree of the following image. It's obvious that the longest simple path is of length 1616.