PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000035. Số đỉnh trên đường đi duy nhất (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
Số đỉnh trên đường đi duy nhất (Tree)
Tree
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree contains vertices numbered to . Each pair has one unique simple path. For every query, count the number of vertices on this path, including both endpoints.
Adding a new edge forms a cycle along the existing path; its number of vertices equals the number of vertices on the path. If , the path contains one vertex.
Input
- First line: .
- Next lines: undirected edges .
- Next line: number of queries .
- Last lines: vertices .
Output
Print the number of vertices on each queried path.
Subtasks
General constraints (all subtasks): ; ; .
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
4
1 2
2 3
2 4
3
3 4
1 3
2 2
Output
3
3
1
Explanation
Path contains 3 vertices, as does ; the path from to itself has 1 vertex.