PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000044. Khoảng cách giữa các đỉnh (Distance Queries)
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 giữa các đỉnh (Distance Queries)
Distance Queries
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree on vertices numbered from to . Every edge has length , and there is exactly one simple path between any two vertices.
For every pair , report the number of edges on their unique path. If , the answer is .
Input
- The first line contains : the numbers of vertices and queries.
- Each of the next lines contains , describing an undirected edge.
- Each of the next lines contains two vertices to query.
Output
Print lines, each containing the number of edges on the corresponding path.
Subtasks
General constraints: ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 3
1 2
1 3
3 4
3 5
1 3
2 5
1 4
Output
1
3
2
Explanation
Vertices and share one edge, giving distance . The path has three edges. Finally, has two edges; hence the three output lines are .