#P9830. Simple Tree Problem
Simple Tree Problem
Simple Tree Problem
Problem Description
There is an undirected tree with vertices and edges. The -th vertex has a positive integer value of (). The -th edge has a positive integer value of (). We define as the total number of vertices in tree with value equal to . We define as the maximum that satisfies is not less than , if there is no that satisfies the condition, then is equal to . For the -th edge, we remove it, the original tree will be divided into two new trees, denoted as and , respectively. For the -th edge, you need to calculate (). $\textbf{Please note that for each edge, we will not really remove it.}$ $\textbf{Please pay attention to the time complexity of your program.}$
Input
Each test contains multiple test cases. The first line of input contains a single integer ——the number of test cases. The description of test cases follows. The first line of each test case contains a single integer —— the number of vertices. The second line of each test case contains integers —— indicating the value of each vertex. The following lines of each test case contains three integers , and —— indicating an edge with value between vertices and . It is guaranteed that the given edges form a tree. It is guaranteed that the sum of does not exceed .
Output
For each testcase, output lines, where the -th line contains an integer representing the answer to the -th edge. Notes: In this problem, you may need more stack space to pass this problem. We suggest you to add the following code into your main function if you use C++.
int main() {
int size(512<<20); // 512M
__asm__ ( "movq %0, %%rsp\n"::"r"((char*)malloc(size)+size));
// YOUR CODE
...
exit(0);
}
And if you use the code above please ; in the end of your main function, otherwise your code may get RE.
Sample Input
3
5
5 2 1 2 2
3 4 2
3 2 1
2 1 1
2 5 1
5
2 1 3 1 5
2 4 1
2 1 2
1 3 2
1 5 3
1
3
Sample Output
2
5
5
5
5
1
1
0
Source
2023“钉耙编程”中国大学生算法设计超级联赛(4)