PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000046. Khoảng cách trên cây có trọng số (Distance in the Tree)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Khoảng cách trên cây có trọng số (Distance in the Tree)
Distance in the Tree
Source: Timus
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected weighted tree contains vertices numbered through . Edge weights are nonnegative integers.
For every queried pair of vertices, calculate the sum of edge weights along their unique path. The distance from a vertex to itself is .
Input
- The first line contains .
- Next lines contain : edge endpoints and weight.
- The next line contains the number of queries .
- The last lines contain pairs .
Output
Print one integer per query on its own line.
Subtasks
General constraints: ; ; ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
3
1 0 1
2 0 1
3
0 1
0 2
1 2
Output
1
1
2
Explanation
The edges and both have length . The first two distances equal . The path contains both edges and has length .