luogu#P9096. [PA 2020] Sen o podboju

[PA 2020] Sen o podboju

Problem Description

This problem is translated from PA 2020 Round 2 Sen o podboju.

King Byteur, the ruler of Byteotia, is currently dreaming of conquering Bitotia. Just like in the real world, in his dream he is still far from defeating the enemy. Therefore, he wants to know what he can do to weaken the enemy country...

In his dream, Bitotia consists of nn cities (numbered from 11 to nn), connected by n1n-1 bidirectional roads, and using only these roads it is possible to travel from any city to any other city. In other words, the map of Bitotia forms a tree. However, Byteur does not remember the exact road network of Bitotia... so his mind creates a random road network.

The king concludes that it is a good idea to force Bitotia to split into kk smaller countries. By a split, Byteur means secretly destroying exactly k1k - 1 roads, which will force Bitotia to decompose into kk smaller countries. These smaller countries are the connected subgraphs formed after removing the selected edges.

However, for the king, destroying just any k1k-1 roads is not enough. Each city of Bitotia has a military coefficient aia_i, also imagined by Byteur. Byteur knows that the stronger the military power of a smaller country, the greater the threat it poses to Byteotia. More precisely: if, in a smaller country, the sum of military coefficients of its cities equals SS, then the threat from this country equals S2S^2. The total threat to Byteotia equals the sum of threats produced by these kk smaller countries.

Now Byteur asks you—his dream (literally!) programmer—for help. Please help him compute the minimum possible total threat after splitting Bitotia into countries. Since Byteur has not yet decided the value of the parameter kk, compute the result for all kk from 11 to nn (inclusive).

Input Format

The first line contains an integer tt, the number of test cases the number of dreams Byteur has. Each test case is described in the same format as follows.

The first line of each test case contains an integer nn.

The second line contains an integer sequence a1,a2,,ana_1,a_2,\cdots ,a_n of length nn.

The next n1n-1 lines describe the road network of Bitotia. Each line contains two integers bi,cib_i,c_i, meaning that cities bib_i and cic_i are connected by a road. It is guaranteed that the input graph is a tree.

Byteur generates the data in the following way. First, he manually chooses an integer tt, an integer interval [nmin,nmax][n_{\min},n_{\max}], and a value amaxa_{\max}. Then, each test case is generated independently by the following steps:

  • The number of cities nn is chosen uniformly at random from the interval [nmin,nmax][n_{\min},n_{\max}].
  • Each aia_i is chosen independently and uniformly at random from the interval [1,amax][1,a_{\max}].
  • Generate a sequence of natural numbers (p1,p2,,pn2)(p_1,p_2,\cdots ,p_{n-2}). Each element of the sequence is chosen independently and uniformly at random from [1,n][1,n]. Byteur uses it as the Prüfer sequence of the road network, i.e., the Prüfer sequence of the tree given in the test case is (p1,p2,,pn2)(p_1,p_2,\cdots,p_{n-2}).

Output Format

Output tt lines, describing the answers for each test case. Each line contains nn integers (where nn is the number of cities in the input). The kk-th integer denotes the minimum threat Bitotia can cause after being split into kk smaller countries.

2
7
9 1 4 2 6 4 7
1 7
6 4
2 3
5 7
3 4
5 3
5
4 8 2 3 1
4 3
3 1
4 2
5 1
1089 545 371 287 227 211 203
324 164 114 102 94

Hint

Explanation of Sample 1

The above testdata was generated using random seed 8 122 0208\ 122\ 020, with parameters t=2,nmin=5,nmax=7,amax=10t=2,n_{\min}=5,n_{\max}=7,a_{\max}=10.

For the first test case, the first number in the output is (9+1+4+2+6+4+7)2=1089(9+1+4+2+6+4+7)^2=1089, which represents the total threat caused by Bitotia when it is not split. The second number in the output corresponds to the total threat if the road connecting cities 55 and 77 is destroyed; in this case, the threat is (9+7)2+(1+4+2+6+4)2=545(9+7)^2+(1+4+2+6+4)^2=545.


Data Generation

The sample generator for this problem is provided in the attachment. The generator takes the following as input: the generator seed and the numbers t,nmin,nmax,amaxt,n_{\min},n_{\max},a_{\max}. All testdata for this problem will be generated using an equivalent generator (i.e., using a different pseudo-random library and independent of the compiler implementation).

To ensure randomness of the tests, the values of t,nmin,nmax,amaxt,n_{\min},n_{\max},a_{\max} for each test case are chosen manually, and the generator seed is chosen randomly.


Constraints

This problem uses bundled tests.

For 100%100\% of the data, it is guaranteed that 1t101\le t\le 10, 2n3002\le n\le 300, 1ai1061\le a_i\le 10^6, 1bi,cin1\le b_i,c_i\le n, 2nminnmax3002\le n_{\min}\le n_{\max}\le 300, 1amax1061\le a_{\max}\le 10^6.

Translated by ChatGPT 5