QOJ.ac

QOJ

実行時間制限: 3.0 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#8720. BFS Order 0

統計

Given a tree with $n$ nodes, rooted at 1.

Perform $q$ queries. Each query provides a sequence, and you need to determine if it is a possible subsequence of a BFS order of the tree.

The definition of a BFS order here is: maintain a queue, initially containing the root. Each time an element $u$ is popped, push all of its children into the queue in some order.

The BFS order can be different for different queries; that is, the queries are independent.

Input

The first line contains a single positive integer $n$.

The second line contains $n - 1$ positive integers, representing the parents of nodes 2 through $n$. It is guaranteed that the parent of a node has a smaller index than the node itself.

The next line contains a single positive integer $q$, representing the number of queries.

The following $q$ lines each start with a positive integer $m$, representing the length of the query sequence, followed by $m$ positive integers representing the query sequence.

Note that the nodes in the query sequence may contain duplicates.

$1 \le n \le 3 \times 10^5$, $1 \le q, \sum m \le 5 \times 10^5$.

Output

Output $q$ lines, each containing either Yes or No, representing your answer.

Case-insensitive.

Examples

Input 1

6
1 1 3 2 4
10
4 3 6 2 5
1 4
3 2 4 5
5 2 5 4 6 3
3 1 4 2
3 5 6 3
5 4 5 2 6 1
4 4 3 2 5
4 4 6 2 3
3 3 2 6

Output 1

No
Yes
Yes
No
No
No
No
No
No
Yes

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.