Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)
H2
Victorious Coloring (Hard Version)
Codeforces
3500
原题
← H1→
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, $q \le 250\,000$. You can hack only if you solved all versions of this problem.

翻译中…

You are given a tree with $n$ vertices, where each vertex is numbered from $1$ to $n$. Each edge is assigned a positive integer weight $w_1, w_2, \ldots, w_{n-1}$ as well.

翻译中…

A victorious coloring is a coloring of each vertex into two colors: red and yellow, where there should be at least one vertex colored in red (corresponding to the symbol of team T1).

翻译中…

Suppose that there is a nonnegative integer weight $x_1, x_2, \ldots, x_n$ assigned to each vertex. The cost of the victorious coloring is defined as the sum of the weights of all red vertices, plus the sum of the weights of all edges that connect vertices of different colors (between red and yellow). We define $f([x_1, x_2, \ldots, x_n])$ as the minimum possible cost for all victorious colorings.

翻译中…

Gumayusi considered the problem of computing $f([x_1, x_2, \ldots, x_n])$, given the sequence $x_1, x_2, \ldots, x_n$. However, this problem was too easy for him, so he devised a variation: Given an integer $l$, find a sequence of nonnegative integer vertex weights $[x_1, x_2, \ldots, x_n]$ such that $f([x_1, x_2, \ldots, x_n]) \ge l$ and the total sum $\sum_{i=1}^n x_i$ is minimized.

翻译中…

Gumayusi was satisfied, but there was a serious issue — this problem doesn't have any queries, which is a necessary component for any problem that isn't bad. So, he added queries to this problem. For each $l$ given as a query, you must find the corresponding minimum possible sum of vertex weights.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line contains an integer $n$ ($2 \le n \le 250\,000$) — the number of vertices.

翻译中…

The following $n-1$ lines contain three integers $u_i$, $v_i$, $w_i$ ($1 \le u_i, v_i \leq n, 1 \le w_i \le 10^9, u_i \neq v_i$) — indicating an edge connecting the vertices $u_i$ and $v_i$ with weight $w_i$.

翻译中…

It is guaranteed that the edges form a tree.

翻译中…

The next line contains an integer $q$ ($1 \le q \le 250\,000$) — the number of queries.

翻译中…

The following $q$ lines contain a single integer $l_i$ ($1 \leq l_i \leq 10^9$) — the parameters of the $i$-th query. It is guaranteed that the sum of $n$ over all test cases does not exceed $250\,000$.

翻译中…

It is guaranteed that the sum of $q$ over all test cases does not exceed $250\,000$.

翻译中…

Output

翻译中…

For each of the $q$ queries, output the answer separated by lines.

翻译中…

Note

翻译中…

The following list shows the possible optimal assignments for each query for the first test case:

翻译中…
  • $[18,24,2,26,18]$
  • $[22,28,6,30,22]$
  • $[4,7,0,9,1]$
  • $[7,13,0,15,7]$
  • $[13,19,0,21,13]$
翻译中…

样例

Input #1
2
5
3 5 10
2 3 4
3 1 10
3 4 2
5
28
32
11
17
23
2
1 2 3
1
1
Output #1
88
108
21
42
66
1
InputCopy OutputCopy
题目信息
题号
2152H2
时间限制
3 s
内存限制
1024 MB
标签
数据结构
贪心
调度

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。