PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000064. Hai đường đi có gặp nhau? (Hamsters Find Sugar)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Hai đường đi có gặp nhau? (Hamsters Find Sugar)
Hamsters Find Sugar
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
A hamster tunnel system is a tree of rooms. For each query, one hamster follows the unique shortest path from to , while another follows the path from to . Determine whether the two paths share at least one vertex. Arrival times are irrelevant.
Input
First line: , then edges and queries .
Output
For each query output Y if the paths intersect, otherwise N.
Subtasks
General constraints: ; ; các cạnh tạo cây.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 5
2 5
4 2
1 3
1 4
5 1 5 1
2 2 1 4
4 1 3 4
3 1 1 5
3 5 1 4
Output
Y
N
Y
Y
Y
Explanation
In the first query both paths are identical. In the second query the first path is just vertex , which does not lie on the path from to , so the answer is N.