CF1901E.Compressed Tree

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a tree consisting of nn vertices. A number is written on each vertex; the number on vertex ii is equal to aia_i .

You can perform the following operation any number of times (possibly zero):

  • choose a vertex which has at most 11 incident edge and remove this vertex from the tree.

Note that you can delete all vertices.

After all operations are done, you're compressing the tree. The compression process is done as follows. While there is a vertex having exactly 22 incident edges in the tree, perform the following operation:

  • delete this vertex, connect its neighbors with an edge.

It can be shown that if there are multiple ways to choose a vertex to delete during the compression process, the resulting tree is still the same.

Your task is to calculate the maximum possible sum of numbers written on vertices after applying the aforementioned operation any number of times, and then compressing the tree.

输入格式

The first line contains a single integer tt ( 1t1041 \le t \le 10^4 ) — the number of test cases.

The first line of each test case contains a single integer nn ( 2n51052 \le n \le 5 \cdot 10^5 ) — the number of vertices.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 109ai109-10^9 \le a_i \le 10^9 ).

Each of the next n1n - 1 lines describes an edge of the tree. Edge ii is denoted by two integers viv_i and uiu_i , the labels of vertices it connects ( 1vi,uin1 \le v_i, u_i \le n , viuiv_i \ne u_i ). These edges form a tree.

Additional constraint on the input: the sum of nn over all test cases doesn't exceed 51055 \cdot 10^5 .

输出格式

For each test case, print a single integer — the maximum possible sum of numbers written on vertices after applying the aforementioned operation any number of times, and then compressing the tree.

输入输出样例

  • 输入#1

    3
    4
    1 -2 2 1
    1 2
    3 2
    2 4
    2
    -2 -5
    2 1
    7
    -2 4 -2 3 3 2 -1
    1 2
    2 3
    3 4
    3 5
    4 6
    4 7

    输出#1

    3
    0
    9
首页