PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000045. Dạo chơi đồng cỏ (Pasture Walk)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Dạo chơi đồng cỏ (Pasture Walk)
Pasture Walk
Source: VNOJ
Version: Phuoc Hung OJ Extended
Problem Statement
A farm contains pastures, numbered from to . Exactly bidirectional roads connect all pastures, forming a tree. Every road has an associated length.
For each pair of pastures requested by the cows, calculate the total length of the unique route connecting them.
Input
- The first line contains .
- The next lines each contain : road endpoints and length.
- The last lines each contain two distinct pastures .
Output
Print the total route length for every query, one integer per line.
Subtasks
General constraints: ; ; ; ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
4 2
2 1 2
4 3 2
1 4 3
1 2
3 2
Output
2
7
Explanation
Pastures and are directly connected by a road of length . The path from to is , whose edge lengths are , totaling .