#LCA0000057. Các truy vấn trên cây (Tree Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Các truy vấn trên cây (Tree Queries)
Tree Queries
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree is rooted at vertex . Each query specifies a set of distinct vertices.
Determine whether some path starting at the root and ending at a vertex has the following property: every specified vertex is either on that path or exactly one edge away from at least one vertex of the path. Print whether such a path exists.
Input
- The first line contains .
- The next lines contain edges .
- Each query line starts with followed by distinct vertex indices.
Output
Print YES or NO for every query, one per line.
Subtasks
General constraints: ; ; tổng trên mọi truy vấn không vượt .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 3
1 2
1 3
2 4
3 5
2 4 5
2 2 4
3 4 5 3
Output
NO
YES
NO
Explanation
For , no root-starting path places both vertices on or within one edge of it; the answer is NO. The path contains both , so query is YES. For , a route through leaves vertex at least two edges away, giving NO.