Hi, little butterfly.
Can I still see you on that page of "once upon a time" today?
Lingluo gives you a labeled rooted tree $T$ of size $n$. The nodes in $T$ are labeled from $1$ to $n$, where the node with label $\text{Rt}$ is the root.
Initially, each node in $T$ has an integer between $1$ and $n$ written on it, called the node's weight. It is guaranteed that the weights of the $n$ nodes form a permutation of $1 \sim n$. Let $a_i$ be the weight of the node with label $i$; then $a_1 \sim a_n$ form a permutation of $1 \sim n$.
Note that for every non-leaf node $u$ in $T$, the children $v_i$ of $u$ are ordered. If $u$ has $k$ children, they are called $\text{ch}(u,1), \text{ch}(u,2), \dots, \text{ch}(u,k)$ from left to right.
Now, perform a DFS on the tree $T$ starting from $\text{Rt}$. When visiting a node $u$, we recursively DFS its $k$ unvisited children in the order $\text{ch}(u,1), \text{ch}(u,2), \dots, \text{ch}(u,k)$. This process yields a unique DFS sequence $s$, where $s_i$ is the label of the $i$-th visited node.
Next, you need to perform exactly $n-1$ edge deletion operations. The $i$-th operation removes the edge $(s_{i+1}, \text{Fa}(s_{i+1}))$ connecting $s_{i+1}$ to its parent $\text{Fa}(s_{i+1})$.
Before the $i$-th edge deletion, you must decide whether to swap the weight of $s_{i+1}$ and the weight of $\text{Fa}(s_{i+1})$. If you choose to swap, the values of $a_{s_{i+1}}$ and $a_{\text{Fa}(s_{i+1})}$ are exchanged, and then the edge $(s_{i+1}, \text{Fa}(s_{i+1}))$ is removed; otherwise, the edge $(s_{i+1}, \text{Fa}(s_{i+1}))$ is removed directly without changing any weights.
You must make $n-1$ choices in total, resulting in $2^{n-1}$ possible schemes. After all $n-1$ edges are removed, let $a'_i$ be the final weight of the node with label $i$. We define the set $S$ as the set of all permutations of final weights that can be obtained through these swap operations. In other words, a permutation $p$ of $1 \sim n$ is in $S$ if and only if there exists at least one sequence of choices such that every final $a'_i = p_i$.
Finally, Lingluo gives you a permutation $p$ of $1 \sim n$. You need to choose one of the following three questions to answer:
- Question 1: Determine whether $p \in S$.
- Question 2: Find the successor of $p$ in $S$. That is, find the lexicographically smallest permutation $q \in S$ such that $q$ is lexicographically greater than $p$. If such a $q$ exists, report its existence and output the smallest such $q$; otherwise, report that it does not exist.
- Question 3: Find the rank of $p$ in $S$. That is, find the number of permutations $q \in S$ such that $q$ is lexicographically smaller than $p$, modulo the large prime $998244353$.
Input
The first line contains two integers $n$ and $\text{Rt}$, representing the size of $T$ and the label of the root node, respectively.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$, representing the initial weights of nodes $1, 2, \dots, n$.
The next $n$ lines describe each node $i$: the first integer $k$ is the number of children of node $i$, followed by $k$ integers $\text{ch}(i,1), \text{ch}(i,2), \dots, \text{ch}(i,k)$, representing the labels of the $1$-st, $2$-nd, $\dots, k$-th children of $i$ from left to right.
The last line contains $n$ integers $p_1, p_2, \dots, p_n$, representing the permutation given by Lingluo.
Output
This problem uses a Special Judge. Your output must contain exactly three lines:
- The first line is the answer to Question 1, which should be the uppercase string
YESorNO. - The second line is the answer to Question 2, which should be $n$ integers $q_1, q_2, \dots, q_n$ forming a permutation of $1 \sim n$. If no permutation $q \in S$ is lexicographically greater than $p$, output a single integer
-1. - The third line is the answer to Question 3, which should be an integer in the range $[0, 998244352]$.
If you do not wish to answer one or more of the questions, you may output the lowercase string no comment on the corresponding line. Ignore trailing spaces, but do not ignore the final newline. There should be exactly one newline after the third line.
In each test case:
- If you answer Question 1 correctly, you will receive $10\%$ of the points for that test case.
- If you answer Question 2 correctly, you will receive $70\%$ of the points for that test case, regardless of whether Question 1 was answered or correct.
- If you answer Question 3 correctly, you will receive $100\%$ of the points for that test case, regardless of whether Questions 1 and 2 were answered or correct.
Note that output not conforming to the format will receive $0$ points! Correct answers, incorrect answers, and no comment are all considered valid formats.
The score for each subtask is the minimum score among all test cases in that subtask.
Examples
Input 1
5 3 2 4 1 3 5 0 1 5 3 4 2 1 0 0 2 5 4 3 1
Output 1
YES 3 4 2 1 5 9
Note 1
The $16$ permutations in $S$ are listed below in lexicographical order, one per line:
1 5 2 3 4
2 1 4 3 5
2 3 4 1 5
2 4 1 3 5
2 4 3 1 5
2 5 1 3 4
2 5 3 1 4
2 5 4 1 3
2 5 4 3 1
3 4 2 1 5
3 5 2 1 4
4 1 2 3 5
4 3 2 1 5
4 5 2 1 3
4 5 2 3 1Additional Examples 1~10
See the provided files.
It is guaranteed that the $1$-st through $10$-th additional examples satisfy the constraints of subtasks $1$ through $10$, respectively.
Subtasks
For all data, it is guaranteed that $2 \leq n \leq 2 \times 10^5$; the children information for the $n$ nodes correctly describes a tree $T$; $a_1 \sim a_n$ and $p_1 \sim p_n$ both form permutations of $1 \sim n$.
The details of each subtask and special properties are as follows:
| Subtask | Score | $n=$ | Tree Shape | Special Property | Dependencies |
|---|---|---|---|---|---|
| $1$ | $5$ | $20$ | |||
| $2$ | $5$ | $8 \times 10^4$ | Complete Binary Tree | DFS order $1 \sim n$ | |
| $3$ | $5$ | $8 \times 10^4$ | Complete Binary Tree | $2$ | |
| $4$ | $5$ | $8 \times 10^4$ | Binary Tree | DFS order $1 \sim n$ | $2$ |
| $5$ | $10$ | $8 \times 10^4$ | Binary Tree | $2 \sim 4$ | |
| $6$ | $10$ | $8 \times 10^4$ | DFS order $1 \sim n$ | $2, 4$ | |
| $7$ | $15$ | $8 \times 10^4$ | $1 \sim 6$ | ||
| $8$ | $15$ | $1.2 \times 10^5$ | $1 \sim 7$ | ||
| $9$ | $15$ | $1.6 \times 10^5$ | $1 \sim 8$ | ||
| $10$ | $15$ | $2 \times 10^5$ | $1 \sim 9$ |
- "Binary Tree": It is guaranteed that every node in $T$ has at most $2$ children.
- "Complete Binary Tree": It is guaranteed that $T$ is isomorphic to a complete binary tree of the same size $n$, and $\text{Rt}$ corresponds to the root of the complete binary tree.
- "DFS order $1 \sim n$": When performing DFS on $T$ as described in the problem, the $i$-th node visited is always the node with label $i$, i.e., $s_i = i$.