当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

6995:Travel on Tree

题目描述
In the world of $\textit{The Three-Body Problem}$, about 200 years later, people will live in huge underground tree buildings. In this problem, we use the tree data structure to describe tree buildings.

There is a tree with $n$ nodes and $n - 1$ edges with length, and $n$ of your friends live in the tree, one at each node. You are planning to visit your friends in the next $m$ days. Each day you choose an interval $[l, r]$ and plan to visit friends living in the nodes numbered from $l$ to $r$. You can choose an arbitrary node $u$ to start the day's visit, then travel on the tree along the edges, and finally go back to $u$. During the travel, you should visit all your friends living in the nodes numbered from $l$ to $r$. You can visit these friends $\textbf{in any order}$ and you can pass a node without visiting the friend. Please calculate the minimum total distance of the travel for each day.
输入解释
The first line of the input contains an integer $T \ (1 \leq T \leq 2 \times 10^4)$ --- the number of test cases.

The first line of each test case contains two integers $n, m \ (1 \leq n, m \leq 10^5)$ --- the number of nodes and the number of days.

Each of the following $n - 1$ lines of each test case contains three integers $u, v, w \ (1 \leq u, v \leq n, 1 \leq w \leq 10^4)$ --- an edge connecting nodes $u$ and $v$ with length $w$.

Each of the following $m$ lines of each test case contains two integers $l, r \ (1 \leq l \leq r \leq n)$ --- a plan to visit your friends living in the nodes numbered from $l$ to $r$.

It is guaranteed that for all test cases, $\sum n \leq 10^6, \ \sum m \leq 10^6$.
输出解释
For each day's plan, output the answer in a single line.
输入样例
2
3 3
1 2 1
2 3 1
1 1
1 2
1 3
5 5
1 2 1
1 3 2
3 4 3
3 5 4
1 3
1 4
1 5
2 5
3 5
输出样例
0
2
4
6
12
20
20
14

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-6995

最后修改于 2021-10-23T19:10:58+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
60000/30000MS(Java/Others) 262144/262144K(Java/Others)