#LCA0000063. Những đường đi có thể đi qua — bản khó (Passable Paths (hard version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những đường đi có thể đi qua — bản khó (Passable Paths (hard version))
Passable Paths (hard version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices labeled through . Each query selects distinct vertices. Decide whether there exists one simple path in the tree containing every selected vertex. Its endpoints may be any vertices in the tree, not necessarily vertices from the query set.
Input
First line: , followed by edges . Next line: . Each query consists of followed by selected vertices.
Output
For every query output YES if the path exists, or NO otherwise. Answers are accepted case-insensitively.
Subtasks
General constraints: ; ; tổng số đỉnh được liệt kê trong các truy vấn không vượt . Trong mỗi truy vấn: , các đỉnh phân biệt.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5
1 2
2 3
2 4
4 5
5
3
3 2 5
5
1 2 3 4 5
2
1 4
3
1 3 5
3
1 5 4
Output
YES
NO
YES
NO
YES
Explanation
Vertices lie on the path . All five vertices cannot fit on a single simple path because of branching at vertex .