#P9678. [ICPC2022 Jinan R] Tree Distance
[ICPC2022 Jinan R] Tree Distance
题目描述
You are given an unrooted weighted tree with vertices . Please answer some queries.
We define as the distance between vertex and vertex in .
For each query, you are given two integers . Please answer the value of
输入格式
The first line contains one integer , the number of vertices in the tree.
Each of the next lines describes an edge of the tree. Edge is denoted by three integers , the labels of vertices it connects and its weight.
Then one line contains one integer , the number of queries.
Each of the following lines contains two integers describing a query.
It is guaranteed that the given edges form a tree.
输出格式
For each query, output the answer in one line. If there is no such that , the answer is .
题目大意
给你一棵 个点的树。记 为树上 之间唯一简单路径的长度。
你要回答 次询问:给定 ,求 $\min\limits_{l\leq i<j\leq r}(\operatorname{dist}(i,j))$。
5
1 2 5
1 3 3
1 4 4
3 5 2
5
1 1
1 4
2 4
3 4
2 5
-1
3
7
7
2