#P17395. [ICPC 2018 Shenyang R] Diameter of a Tree

    ID: 19676 远端评测题 14000ms 512MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>2018二分点分治分治树的直径凸包ICPC闵可夫斯基和 Minkowski sum

[ICPC 2018 Shenyang R] Diameter of a Tree

Problem Description

Given a weighted tree T0T_0, we define its diameter as the length of the longest path between two vertices on it, where the length of a path is equal to the sum of weights of edges in the path.

If the tree stretches like a spider or a yawning tiger, its diameter will change. Let's use TiT_i to denote the state of the tree after ii seconds, at which time the weight of each edge has been increased by exactly ii units from T0T_0.

You will be presented with several different queries, and you should calculate the tree's diameter at some specified time for each query.

Input Format

The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 6060.

For each test case, the first line contains two integers nn and mm indicating the number of vertices in the tree and the number of given queries respectively, where 2n2×1052 \le n \le 2 \times 10^5 and 1m2×1051 \le m \le 2 \times 10^5.

Each of the following (n1)(n - 1) lines contains three integers u,vu, v and ww which represent an edge in the original tree between the uu-th vertex and the vv-th one of weight ww, where 1u,vn1 \le u, v \le n, uvu \ne v and 1w1081 \le w \le 10^8.

Each of the following mm lines describes a query containing only one integer kk that asks you to calculate the diameter of the tree TkT_k, where 0k1090 \le k \le 10^9.

We guarantee that the sum of nn in all test cases is no larger than 10610^6, and the sum of mm in all test cases is no larger than 10610^6 as well.

Output Format

For each test case, output a line containing "Case #x:" (without quotes) at first, where x\text{x} is the test case number starting from 11.

Then output mm lines corresponding to all queries. The ii-th line of them contains an integer indicating the answer to the ii-th query.

2
3 3
1 2 1
1 3 5
5
10
100
5 6
1 2 100
2 3 5
2 4 1
4 5 1
0
1
2
3
4
5
Case #1:
16
26
206
Case #2:
105
107
109
111
114
117

Hint

In the second sample case:

  • the diameter of T0T_0 is 105105, which is the length of the path 1231 - 2 - 3; and
  • the diameter of T5T_5 is 117117, which is the length of the path 12451 - 2 - 4 - 5.