#P2912. [USACO08OCT] Pasture Walking G
[USACO08OCT] Pasture Walking G
Description
The cows (), conveniently numbered …, are grazing among the pastures also conveniently numbered …. Most conveniently of all, cow is grazing in pasture .
Some pairs of pastures are connected by one of bidirectional walkways that the cows can traverse. Walkway connects pastures and () and has a length of ().
The walkways are set up so that between any two distinct pastures, there is exactly one path of walkways that travels between them. Thus, the walkways form a tree.
The cows are very social and wish to visit each other often. Being in a hurry, they want you to help schedule their visits by computing the lengths of the paths for pairs of pastures (). Each pair is given as a query , ().
Input Format
- Line 1: Two space-separated integers and .
- Lines …: Line contains three space-separated integers , , and .
- Lines …: Each line contains two space-separated integers representing two distinct pastures between which the cows wish to travel: and .
Output Format
- Lines …: Line contains the length of the path between the two pastures in query .
4 2
2 1 2
4 3 2
1 4 3
1 2
3 2
2
7
Hint
Query 1: The walkway between pastures and has length .
Query 2: Travel through the walkway between pastures and , then the one between and , and finally the one between and , for a total length of .
Translated by ChatGPT 5
京公网安备 11011102002149号