问题 B: Simple Tree Problem

内存限制:256 MB 时间限制:2 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

There is an undirected tree with n vertices and n1 edges.

The i-th vertex has a positive integer value of ai(i=1,2,,n).

The i-th edge has a positive integer value of ki(i=1,2,,n1).

We definef(x,T) as the total number of vertices in tree T with value equal to x.

We define g(y,T) as the maximum x that satisfies f(x,T) is not less than y, if there is no x that satisfies the condition, then g(y,T) is equal to 00.

For the i-th edge, if we remove it, the original tree will be divided into two new trees, denoted as Ti1 and Ti2, respectively.

For the i-th edge, you need to calculate max,max(g(ki,Ti1),g(ki,Ti2))(i=1,2,,n1).

Please note that for each edge, we will not really remove it.Please note that for each edge, we will not really remove it.

Please pay attention to the time complexity of your program.Please pay attention to the time complexity of your program.

输入格式

Each test contains multiple test cases. The first line of input contains a single integer t(1t10^6)——the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n(1n10^6) —— the number of vertices.

The second line of each test case contains n integers ai(1ai10^9) —— indicating the value of each vertex.

The following n1 lines of each test case contains three integers ui,vi and ki (1ui,vi,kin,ui=vi) —— indicating an edge with value ki between vertices ui and vi. It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of n does not exceed 3×10^6.

输出格式

For each testcase, output n1 lines, where the i-th line contains an integer representing the answer to the i-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 DON’T forget to add exit(0)DON’T forget to add exit(0); in the end of your main function, otherwise your code may get RE.

输入样例 复制

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

输出样例 复制

2
5
5
5
5
1
1
0