#P9058. [Ynoi2004] rpmtdq
[Ynoi2004] rpmtdq
Problem Description
Given an unrooted tree with edge weights, you need to answer some queries.
Define as the distance between node and node on the tree.
For each query, you are given . You need to output where .
Input Format
The first line contains an integer , denoting the number of nodes in the tree.
The next lines each contain three integers , representing a tree edge connecting and with weight . The input is guaranteed to form a tree.
Then there is a line containing an integer , denoting the number of queries.
The next lines each contain two integers , representing a query. If for a query, there is no pair satisfying , output .
Output Format
Output lines. Each line contains one integer, denoting the answer to the corresponding query.
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
Hint
Idea: nzhtl1477, Solution: Kubic&ccz181078, Code: Kubic, Data: Kubic.
For of the testdata, , , and .
Translated by ChatGPT 5