CF1866K.Keen Tree Calculation

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a tree of NN vertices and N−1N-1 edges. The ii-th edge connects vertices UiU_i and ViV_i and has a length of WiW_i.

Chaneka, the owner of the tree, asks you QQ times. For the jj-th question, the following is the question format:

  • XjX_j KjK_j – If each edge that contains vertex XjX_j has its length multiplied by KjK_j, 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.

有一棵包含 NN 个顶点和 N−1N-1 条边的树。第 ii 条边连接顶点 UiU_i 和 ViV_i,长度为 WiW_i。

树的所有者 Chaneka 向你提出 QQ 个问题。对于第 jj 个问题,其格式如下:

  • XjX_j KjK_j —— 若将所有与顶点 XjX_j 相连的边的长度均乘以 KjK_j,则该树的直径是多少?

注意事项:

  • Chaneka 的每个问题相互独立,即边长的修改不会影响后续问题。
  • 树的直径是指树中任意两个不同顶点之间可能的最大距离。

输入格式

The first line contains a single integer NN (2≤N≤1052\leq N\leq10^5) — the number of vertices in the tree.

The ii-th of the next N−1N-1 lines contains three integers UiU_i, ViV_i, and WiW_i (1≤Ui,Vi≤N1 \leq U_i,V_i \leq N; 1≤Wi≤1091\leq W_i\leq10^9) — an edge that connects vertices UiU_i and ViV_i with a length of WiW_i. The edges form a tree.

The (N+1)(N+1)-th line contains a single integer QQ (1≤Q≤1051\leq Q\leq10^5) — the number of questions.

The jj-th of the next QQ lines contains two integers XjX_j and KjK_j as described (1≤Xj≤N1 \leq X_j \leq N; 1≤Kj≤1091 \leq K_j \leq 10^9).

第一行包含一个整数 NN(2≤N≤1052\leq N\leq10^5)——树中顶点的数量。

接下来的 N−1N-1 行中,第 ii 行包含三个整数 UiU_i、ViV_i 和 WiW_i(1≤Ui,Vi≤N1 \leq U_i,V_i \leq N;1≤Wi≤1091\leq W_i\leq10^9)——表示一条连接顶点 UiU_i 与 ViV_i、长度为 WiW_i 的边。这些边构成一棵树。

第 N+1N+1 行包含一个整数 QQ(1≤Q≤1051\leq Q\leq10^5)——询问的数量。

接下来的 QQ 行中,第 jj 行包含两个整数 XjX_j 和 KjK_j(如题面所述)(1≤Xj≤N1 \leq X_j \leq N;1≤Kj≤1091 \leq K_j \leq 10^9)。

输出格式

Output QQ lines with an integer in each line. The integer in the jj-th line represents the diameter of the tree on the jj-th question.

输出 QQ 行,每行一个整数。第 jj 行的整数表示第 jj 个询问中树的直径。

输入输出样例

  • 输入#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 11-st question.

The maximum distance is between vertices 66 and 77, which is 6+6+6=186+6+6=18, so the diameter is 1818.

The following is the tree on the 22-nd question.

The maximum distance is between vertices 22 and 66, which is 3+2+6=113+2+6=11, so the diameter is 1111.

在第一个例子中,以下是未作任何修改的树:

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

最大距离出现在顶点 66 与 77 之间,其值为 6+6+6=186+6+6=18,因此直径为 1818。

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

最大距离出现在顶点 22 与 66 之间,其值为 3+2+6=113+2+6=11,因此直径为 1111。

输入解题思路,AI测评打分。不知道怎么写?

首页