CF526G.Spiders Evil Plan

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Spiders are Om Nom's old enemies. They love eating candies as much as he does and that's why they keep trying to keep the monster away from his favorite candies. They came up with an evil plan to trap Om Nom.

Let's consider a rope structure consisting of nn nodes and n1n-1 ropes connecting the nodes. The structure is connected, thus, the ropes and the nodes form a tree. Each rope of the formed structure is associated with its length. A candy is tied to node xx of the structure. Om Nom really wants to eat this candy.

The yy spiders are trying to stop him from doing it. They decided to entangle the candy and some part of the structure into a web, thus attaching the candy to as large as possible part of the rope structure.

Each spider can use his web to cover all ropes on the path between two arbitrary nodes aa and bb . Thus, yy spiders can cover the set of ropes which is a union of yy paths in the given tree. These yy paths can arbitrarily intersect each other. The spiders want the following conditions to be hold:

  • the node containing the candy is adjacent to at least one rope covered with a web
  • the ropes covered with the web form a connected structure (what's the idea of covering with a web the ropes that are not connected with the candy?)
  • the total length of the ropes covered with web is as large as possible

The spiders haven't yet decided to what node of the structure they will tie the candy and how many spiders will cover the structure with web, so they asked you to help them. Help them calculate the optimal plan for multiple values of xx and yy .

输入格式

The first line contains numbers nn and qq ( 1<=n,q<=1051<=n,q<=10^{5} ) — the number of nodes in the structure and the number of questions that the spiders want to ask you.

The next n1n-1 lines determine the rope structure. The ii -th line contains three integers ui,vi,liu_{i},v_{i},l_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n , uiviu_{i}≠v_{i} , 1<=li<=10001<=l_{i}<=1000 ), showing that there is a rope of length lil_{i} between nodes uiu_{i} and viv_{i} .

Next qq lines describe the spiders' questions. As they want you to answer their question online, they encoded their messages in a special manner.

Each of the next qq lines contains two numbers xi,yix_{i},y_{i} . In the first question of the spiders x=x1,y=y1x=x_{1},y=y_{1} .

To calculate values xx and yy in the spiders' ii -th ( 2<=i<=q2<=i<=q ) question, you need to use the following formulas:

where Ansi1Ans_{i-1} is the total length of the ropes covered by a web in the answer for the (i1)(i-1) -th question.

The following inequality holds: 1<=xi,yi<=n1<=x_{i},y_{i}<=n .

输出格式

For each question of the spiders print on a separate line a single integer AnsiAns_{i} — the total length of the ropes covered with web in the optimal plan.

输入输出样例

  • 输入#1

    6 3
    1 2 2
    2 3 2
    3 4 2
    4 6 1
    3 5 10
    3 1
    2 5
    1 1
    

    输出#1

    14
    13
    17
    
首页