CF1866K.Keen Tree Calculation
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a tree of N vertices and N−1 edges. The i-th edge connects vertices Ui and Vi and has a length of Wi.
Chaneka, the owner of the tree, asks you Q times. For the j-th question, the following is the question format:
- Xj Kj – If each edge that contains vertex Xj has its length multiplied by Kj, what is the diameter of the tree?
Notes:
- Each of Chaneka's question is independent, which means the changes in edge length do not influence the next questions.
- The diameter of a tree is the maximum possible distance between two different vertices in the tree.
有一棵包含 N 个顶点和 N−1 条边的树。第 i 条边连接顶点 Ui 和 Vi,长度为 Wi。
树的所有者 Chaneka 向你提出 Q 个问题。对于第 j 个问题,其格式如下:
- Xj Kj —— 若将所有与顶点 Xj 相连的边的长度均乘以 Kj,则该树的直径是多少?
注意事项:
- Chaneka 的每个问题相互独立,即边长的修改不会影响后续问题。
- 树的直径是指树中任意两个不同顶点之间可能的最大距离。
输入格式
The first line contains a single integer N (2≤N≤105) — the number of vertices in the tree.
The i-th of the next N−1 lines contains three integers Ui, Vi, and Wi (1≤Ui,Vi≤N; 1≤Wi≤109) — an edge that connects vertices Ui and Vi with a length of Wi. The edges form a tree.
The (N+1)-th line contains a single integer Q (1≤Q≤105) — the number of questions.
The j-th of the next Q lines contains two integers Xj and Kj as described (1≤Xj≤N; 1≤Kj≤109).
第一行包含一个整数 N(2≤N≤105)——树中顶点的数量。
接下来的 N−1 行中,第 i 行包含三个整数 Ui、Vi 和 Wi(1≤Ui,Vi≤N;1≤Wi≤109)——表示一条连接顶点 Ui 与 Vi、长度为 Wi 的边。这些边构成一棵树。
第 N+1 行包含一个整数 Q(1≤Q≤105)——询问的数量。
接下来的 Q 行中,第 j 行包含两个整数 Xj 和 Kj(如题面所述)(1≤Xj≤N;1≤Kj≤109)。
输出格式
Output Q lines with an integer in each line. The integer in the j-th line represents the diameter of the tree on the j-th question.
输出 Q 行,每行一个整数。第 j 行的整数表示第 j 个询问中树的直径。
输入输出样例
输入#1
7 5 1 2 1 4 2 3 4 1 2 5 3 6 1 6 4 7 2 2 4 3 3 2
输出#1
18 11
输入#2
3 1 2 1000000000 2 3 1000000000 1 2 1000000000
输出#2
2000000000000000000
说明/提示
In the first example, the following is the tree without any changes.

The following is the tree on the 1-st question.

The maximum distance is between vertices 6 and 7, which is 6+6+6=18, so the diameter is 18.
The following is the tree on the 2-nd question.

The maximum distance is between vertices 2 and 6, which is 3+2+6=11, so the diameter is 11.
在第一个例子中,以下是未作任何修改的树:

以下是第 1 个询问时的树:

最大距离出现在顶点 6 与 7 之间,其值为 6+6+6=18,因此直径为 18。
以下是第 2 个询问时的树:

最大距离出现在顶点 2 与 6 之间,其值为 3+2+6=11,因此直径为 11。
输入解题思路,AI测评打分。不知道怎么写?