CF1866K.Keen Tree Calculation

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

There is a tree of NN vertices and N1N-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.

输入格式

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

The ii -th of the next N1N-1 lines contains three integers UiU_i , ViV_i , and WiW_i ( 1Ui,ViN1 \leq U_i,V_i \leq N ; 1Wi1091\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 ( 1Q1051\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 ( 1XjN1 \leq X_j \leq N ; 1Kj1091 \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.

输入输出样例

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

首页