Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ 9 の非負整数列 A=(A_1,A_2,\dots,A_9) が与えられます。ここで、2\leq \sum_{i=1}^{9}A_i が保証されます。
あなたは、A の要素を 1 つ選び 1 加算する操作を 0 回以上好きな回数行うことができます。
以下の条件をすべて満たす正整数列 S が存在するようにするために、最小で何回の操作が必要ですか。
- S の要素はすべて 1 以上 9 以下である。
- 1\leq i\leq 9 なる整数 i について、S に i がちょうど A_i 個含まれる。(したがって、S の長さは \sum_{i=1}^{9}A_i である。)
- どの隣接する 2 要素の和も 10 にならない。
ただし、有限回の操作で目標を達成できることが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 10^3
- 0\leq A_i\leq 10^9 (1\leq i\leq 9)
- 2\leq \sum_{i=1}^{9}A_i
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
A_1 A_2 \dots A_9
出力
T 行出力せよ。
i 行目には i 番目のテストケースについて、答えを出力せよ。
入力例 1
2 0 0 0 1 0 1 0 0 0 2 1 1 0 0 0 0 0 0
出力例 1
1 0
1 番目のテストケースについて、はじめの状態では、最後の条件以外を満たす S としてあり得るものは (4,6) または (6,4) で、いずれも最後の条件を満たしません。A_8 を選び 1 加算することで、S=(6,8,4) がすべての条件を満たすようになります。
2 番目のテストケースについて、操作をするまでもなく S=(1,2,3,1) が条件を満たしています。
Score : 500 points
Problem Statement
You are given a length-9 sequence of non-negative integers A=(A_1,A_2,\dots,A_9). It is guaranteed that 2\leq \sum_{i=1}^{9}A_i.
You can perform the following operation any number of times (possibly zero): choose one element of A and add 1 to it.
What is the minimum number of operations required so that there exists a sequence of positive integers S satisfying all of the following conditions?
- All elements of S are between 1 and 9, inclusive.
- S contains exactly A_i occurrences of i for each integer i such that 1\leq i\leq 9. (Thus, the length of S is \sum_{i=1}^{9}A_i.)
- No two adjacent elements sum to 10.
It can be proved that the goal can be achieved in a finite number of operations.
You are given T test cases; solve each of them.
Constraints
- 1\leq T\leq 10^3
- 0\leq A_i\leq 10^9 (1\leq i\leq 9)
- 2\leq \sum_{i=1}^{9}A_i
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
A_1 A_2 \dots A_9
Output
Output T lines.
The i-th line should contain the answer for the i-th test case.
Sample Input 1
2 0 0 0 1 0 1 0 0 0 2 1 1 0 0 0 0 0 0
Sample Output 1
1 0
For the first test case, initially, the possible values of S satisfying all conditions except the last one are (4,6) and (6,4), neither of which satisfies the last condition. By choosing A_8 and adding 1 to it, S=(6,8,4) satisfies all conditions.
For the second test case, S=(1,2,3,1) satisfies the conditions without any operations.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
X\leq Y\leq Z なる正整数の組 (X,Y,Z) が与えられます。
非負整数列の組 (S_1,S_2,S_3) であって、以下の条件をすべて満たすものを 1 つ出力してください。
- S_1,S_2,S_3 の長さはそれぞれ 1 以上 1000 以下
- S_1 の連続部分列であり、かつ S_2 の連続部分列であるもののうち、最長のものの長さが X
- S_1 の連続部分列であり、かつ S_3 の連続部分列であるもののうち、最長のものの長さが Y
- S_2 の連続部分列であり、かつ S_3 の連続部分列であるもののうち、最長のものの長さが Z
- 上 4 つの条件をすべて満たすもののうち、\max(\max(S_1),\max(S_2),\max(S_3)) が最小
本問題の制約下で、条件を満たす (S_1,S_2,S_3) は必ず存在することが証明できます。そのようなものが複数ある場合、どれを出力してもかまいません。
制約
- 1\leq X\leq Y\leq Z\leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
X Y Z
出力
3 行出力せよ。
1 行目には、S_1 を以下の形式で出力せよ。ただし、|S_1| で S_1 の長さを、S_{1,i} で S_1 の i 番目の要素を表す。
|S_1| S_{1,1} S_{1,2} \dots S_{1,|S_1|}
2 行目には S_2 を、3 行目には S_3 を S_1 と同様の形式で出力せよ。
解が複数ある場合、どれを出力してもかまわない。
入力例 1
4 5 5
出力例 1
10 1 1 0 0 1 0 1 0 1 1 8 1 1 0 1 0 0 1 1 11 1 0 0 0 1 1 0 1 0 1 0
S_1=(1,1,0,0,1,0,1,0,1,1),S_2=(1,1,0,1,0,0,1,1),S_3=(1,0,0,0,1,1,0,1,0,1,0) が条件を満たすことは以下のように確認できます。
- S_1,S_2,S_3 の長さはそれぞれ 10,8,11 であり、すべて 1 以上 1000 以下である。
- S_1 の連続部分列であり、かつ S_2 の連続部分列であるもののうち、最長のものは (1,0,0,1) または (1,0,1,0) であり、その長さは X=4 である。
- S_1 の連続部分列であり、かつ S_3 の連続部分列であるもののうち、最長のものは (1,0,1,0,1) または (0,1,0,1,0) であり、その長さは Y=5 である。
- S_2 の連続部分列であり、かつ S_3 の連続部分列であるもののうち、最長のものは (1,1,0,1,0) であり、その長さは Z=5 である。
- \max(\max(S_1),\max(S_2),\max(S_3))=1 であり、\max(\max(S_1),\max(S_2),\max(S_3))=0 として上 4 つの条件を満たすことはできないことが示せるため、これが最小である。
Score : 500 points
Problem Statement
You are given a triple of positive integers (X,Y,Z) such that X\leq Y\leq Z.
Output one triple of sequences of non-negative integers (S_1,S_2,S_3) that satisfies all of the following conditions:
- The lengths of S_1,S_2,S_3 are each between 1 and 1000, inclusive.
- The length of a longest sequence that is both a contiguous subsequence of S_1 and a contiguous subsequence of S_2 is X.
- The length of a longest sequence that is both a contiguous subsequence of S_1 and a contiguous subsequence of S_3 is Y.
- The length of a longest sequence that is both a contiguous subsequence of S_2 and a contiguous subsequence of S_3 is Z.
- \max(\max(S_1),\max(S_2),\max(S_3)) is minimized among all triples satisfying the above four conditions.
It can be proved that under the constraints of this problem, a triple (S_1,S_2,S_3) satisfying the conditions always exists. If there are multiple such triples, you may output any of them.
Constraints
- 1\leq X\leq Y\leq Z\leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
X Y Z
Output
Output three lines.
The first line should contain S_1 in the following format, where |S_1| denotes the length of S_1 and S_{1,i} denotes the i-th element of S_1:
|S_1| S_{1,1} S_{1,2} \dots S_{1,|S_1|}
The second line should contain S_2 and the third line should contain S_3, both in the same format as S_1.
If there are multiple solutions, you may output any of them.
Sample Input 1
4 5 5
Sample Output 1
10 1 1 0 0 1 0 1 0 1 1 8 1 1 0 1 0 0 1 1 11 1 0 0 0 1 1 0 1 0 1 0
It can be verified that S_1=(1,1,0,0,1,0,1,0,1,1),S_2=(1,1,0,1,0,0,1,1),S_3=(1,0,0,0,1,1,0,1,0,1,0) satisfies the conditions as follows:
- The lengths of S_1,S_2,S_3 are 10,8,11, respectively, all of which are between 1 and 1000, inclusive.
- The longest sequences that are both a contiguous subsequence of S_1 and a contiguous subsequence of S_2 are (1,0,0,1) and (1,0,1,0), with length X=4.
- The longest sequences that are both a contiguous subsequence of S_1 and a contiguous subsequence of S_3 are (1,0,1,0,1) and (0,1,0,1,0), with length Y=5.
- The longest sequence that is both a contiguous subsequence of S_2 and a contiguous subsequence of S_3 is (1,1,0,1,0), with length Z=5.
- \max(\max(S_1),\max(S_2),\max(S_3))=1, and it can be shown that it is impossible to satisfy the above four conditions with \max(\max(S_1),\max(S_2),\max(S_3))=0, so this is minimal.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
正整数 N、長さ N の # と . からなる文字列 S、長さ N の正整数列 R=(R_1,R_2,\dots,R_N) が与えられます。
N 個の区画が横 1 列に並んだ森林があります。区画には左から順に 1,2,\dots,N と番号が付けられています。
いくつかの区画には木が生えています。具体的には、S の i 文字目が # であるとき、またそのときに限り区画 i には木が生えています。ここで、次に示す操作を 1 回以上行うことができることが保証されます。
あなたは、以下の操作を好きなだけ繰り返すことができます。
- 木が生えていない区画を 2 つ選ぶ。選んだ区画を左から区画 i,j とすると、区画 i と区画 j の間にある木がすべてなくなり、R_i+R_j の報酬を得る。
ただし、木が 1 本もなくならないような操作を行うことはできません。
最終的に獲得できる報酬の総和の最大値を達成するために、最初に行うことのできる操作の数を求めてください。ただし、2 つの操作は、どちらか一方のみで選んでいる区画が存在するとき、またそのときに限り区別します。
制約
- 3\leq N\leq 2\times 10^5
- S の長さは N で、各文字は
#または. - 1\leq R_i\leq 10^9 (1\leq i\leq N)
- 操作を 1 回以上行うことができる
- N,R_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S R_1 R_2 \dots R_N
出力
答えを出力せよ。
入力例 1
8 ...###.. 20 25 1 1 30 2 1 1
出力例 1
2
最初に行うことのできる操作は以下の 6 通りです。
- 区画 1 と区画 7 を選ぶ。区画 4,5,6 にある木がなくなり、21 の報酬を得る。
- 区画 1 と区画 8 を選ぶ。区画 4,5,6 にある木がなくなり、21 の報酬を得る。
- 区画 2 と区画 7 を選ぶ。区画 4,5,6 にある木がなくなり、26 の報酬を得る。
- 区画 2 と区画 8 を選ぶ。区画 4,5,6 にある木がなくなり、26 の報酬を得る。
- 区画 3 と区画 7 を選ぶ。区画 4,5,6 にある木がなくなり、2 の報酬を得る。
- 区画 3 と区画 8 を選ぶ。区画 4,5,6 にある木がなくなり、2 の報酬を得る。
どの操作を行ったとしてもすべての木がなくなるので、2 回目の操作を行うことはできません。獲得できる報酬の総和の最大値は 26 です。
入力例 2
5 .#.#. 211 182 192 182 211
出力例 2
2
獲得できる報酬の総和の最大値は 825 です。
最初に得る報酬を最大化するのではないことに注意してください。最初に区画 1 と区画 5 を選ぶと 422 の報酬を獲得できますが、その後操作を行うことができず、報酬の総和を 825 にすることができません。
入力例 3
11 #..#.##.#.. 192 192 192 211 182 192 182 192 182 211 182
出力例 3
3
Score : 600 points
Problem Statement
You are given a positive integer N, a string S of length N consisting of # and ., and a length-N sequence of positive integers R=(R_1,R_2,\dots,R_N).
There is a forest with N sections arranged in a single horizontal line. The sections are numbered 1,2,\dots,N from left to right.
Some sections have trees. Specifically, section i has a tree if and only if the i-th character of S is #. It is guaranteed that the following operation can be performed at least once.
You can repeat the following operation as many times as you like:
- Choose two sections without trees. Let sections i and j (where i < j) be the chosen sections from left to right. Then, all trees between sections i and j are removed, and you gain a reward of R_i+R_j.
Here, you cannot perform an operation that removes no trees.
Find the number of ways to perform the first operation to achieve the maximum possible total reward. Two ways are considered distinct if and only if there exists a section chosen in one way but not in the other.
Constraints
- 3\leq N\leq 2\times 10^5
- The length of S is N, and each character is
#or.. - 1\leq R_i\leq 10^9 (1\leq i\leq N)
- The operation can be performed at least once.
- N and R_i are integers.
Input
The input is given from Standard Input in the following format:
N S R_1 R_2 \dots R_N
Output
Output the answer.
Sample Input 1
8 ...###.. 20 25 1 1 30 2 1 1
Sample Output 1
2
There are six ways to perform the first operation, as follows:
- Choose sections 1 and 7. The trees at sections 4,5,6 are removed, and you gain a reward of 21.
- Choose sections 1 and 8. The trees at sections 4,5,6 are removed, and you gain a reward of 21.
- Choose sections 2 and 7. The trees at sections 4,5,6 are removed, and you gain a reward of 26.
- Choose sections 2 and 8. The trees at sections 4,5,6 are removed, and you gain a reward of 26.
- Choose sections 3 and 7. The trees at sections 4,5,6 are removed, and you gain a reward of 2.
- Choose sections 3 and 8. The trees at sections 4,5,6 are removed, and you gain a reward of 2.
No matter which operation is performed, all trees are removed, so a second operation cannot be performed. The maximum possible total reward is 26.
Sample Input 2
5 .#.#. 211 182 192 182 211
Sample Output 2
2
The maximum possible total reward is 825.
Note that the goal is not to maximize the reward obtained in the first operation. If you choose sections 1 and 5 first, you can gain a reward of 422, but no operation can be performed afterwards, and the total reward cannot reach 825.
Sample Input 3
11 #..#.##.#.. 192 192 192 211 182 192 182 192 182 211 182
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
N 頂点 M 辺の単純連結無向グラフが与えられます。i 番目 (1\leq i\leq M) の辺は頂点 U_i と頂点 V_i を結ぶ無向辺です。
すべての頂点に、以下のように道標を立てます。
- 頂点 1 以外のすべての頂点に、隣接している頂点のうちどれか 1 つを指す青色の道標を立てる。
- 頂点 2 以外のすべての頂点に、隣接している頂点のうちどれか 1 つを指す黄色の道標を立てる。
ただし、同じ頂点にある 2 つの道標が同じ頂点を指してはいけません。
以下の条件を満たすように道標を立てることは可能か判定し、可能なら道標の立て方を 1 つ出力してください。
- 頂点 1 以外のどの頂点からも、青色の道標の指す頂点に動くことを有限回繰り返すことで頂点 1 に到達することができる。
- 頂点 2 以外のどの頂点からも、黄色の道標の指す頂点に動くことを有限回繰り返すことで頂点 2 に到達することができる。
条件を満たす道標の立て方が複数ある場合、どれを出力してもかまいません。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq M\leq 3\times 10^5
- 1\leq U_i\lt V_i\leq N (1\leq i\leq M)
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
条件を満たす道標の立て方が存在しない場合、No とだけ出力せよ。
存在する場合、N+1 行出力せよ。
1 行目には、Yes と出力せよ。
2 行目には、頂点 1 に立てる黄色の道標が指す頂点の番号を出力せよ。
3 行目には、頂点 2 に立てる青色の道標が指す頂点の番号を出力せよ。
i 行目 (4\leq i\leq N+1) には、頂点 i-1 に立てる青色の道標が指す頂点の番号、黄色の道標が指す頂点の番号をこの順に空白区切りで出力せよ。
解が複数ある場合、どれを出力してもかまわない。
入力例 1
7 8 1 3 3 4 4 5 1 5 4 6 2 6 2 7 4 7
出力例 1
Yes 5 6 4 1 5 6 1 4 4 2 4 2
下図のような道標の立て方です。たとえば、頂点 3 からは
- 青色の道標に沿うと、頂点 3\rightarrow 4\rightarrow 5\rightarrow 1 と動き、最終的に頂点 1 に到達します。
- 黄色の道標に沿うと、頂点 3\rightarrow 1\rightarrow 5\rightarrow 4 \rightarrow 6\rightarrow 2 と動き、最終的に頂点 2 に到達します。
また、頂点 2 から青色の道標に沿って移動すると頂点 1 に到達できること、頂点 1 から黄色の道標に沿って移動すると頂点 2 に到達できることも確かめることができます。その他の頂点も条件を満たします。

この他にも複数の解がありますが、頂点 3 にある青色の道標と黄色の道標をともに頂点 4 に向けてはいけません。同じ頂点にある道標が同じ頂点を指してしまうためです。
入力例 2
10 12 1 3 3 4 4 5 1 5 4 6 2 6 2 7 4 7 4 8 8 9 9 10 8 10
出力例 2
No
条件を満たす道標の立て方は存在しません。
Score : 700 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges. The i-th edge (1\leq i\leq M) is an undirected edge connecting vertices U_i and V_i.
Place signposts at all vertices as follows:
- At every vertex except vertex 1, place a blue signpost pointing to one of its adjacent vertices.
- At every vertex except vertex 2, place a yellow signpost pointing to one of its adjacent vertices.
Here, the two signposts at the same vertex must not point to the same vertex.
Determine whether it is possible to place signposts satisfying the following conditions, and if possible, output one way to place them:
- From any vertex other than vertex 1, you can reach vertex 1 by repeatedly moving to the vertex pointed to by the blue signpost a finite number of times.
- From any vertex other than vertex 2, you can reach vertex 2 by repeatedly moving to the vertex pointed to by the yellow signpost a finite number of times.
If there are multiple ways to place signposts satisfying the conditions, you may output any of them.
Constraints
- 2\leq N\leq 2\times 10^5
- 1\leq M\leq 3\times 10^5
- 1\leq U_i\lt V_i\leq N (1\leq i\leq M)
- The given graph is simple and connected.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
If there is no way to place signposts satisfying the conditions, output No only.
If there is, output N+1 lines.
The first line should contain Yes.
The second line should contain the number of the vertex pointed to by the yellow signpost at vertex 1.
The third line should contain the number of the vertex pointed to by the blue signpost at vertex 2.
The i-th line (4\leq i\leq N+1) should contain the number of the vertex pointed to by the blue signpost at vertex i-1 and the number of the vertex pointed to by the yellow signpost at vertex i-1, in this order, separated by a space.
If there are multiple solutions, you may output any of them.
Sample Input 1
7 8 1 3 3 4 4 5 1 5 4 6 2 6 2 7 4 7
Sample Output 1
Yes 5 6 4 1 5 6 1 4 4 2 4 2
Here, signposts are placed as shown in the figure below. For example, from vertex 3:
- Following the blue signpost, you move as vertex 3\rightarrow 4\rightarrow 5\rightarrow 1, eventually reaching vertex 1.
- Following the yellow signpost, you move as vertex 3\rightarrow 1\rightarrow 5\rightarrow 4 \rightarrow 6\rightarrow 2, eventually reaching vertex 2.
You can also verify that following the blue signpost from vertex 2 leads to vertex 1, and following the yellow signpost from vertex 1 leads to vertex 2. The other vertices also satisfy the conditions.

There are multiple other solutions, but you must not have both the blue signpost and the yellow signpost at vertex 3 point to vertex 4, because the signposts at the same vertex would point to the same vertex.
Sample Input 2
10 12 1 3 3 4 4 5 1 5 4 6 2 6 2 7 4 7 4 8 8 9 9 10 8 10
Sample Output 2
No
There is no way to place signposts satisfying the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
正整数 N と、(1,2,\dots,N) の順列 P が与えられます。
頂点に 1,2,\dots,N と番号がついた N 頂点の木があるとき、すぬけくんと高橋くんは以下の規則で動きます。
- すぬけくんは、高橋くんが以下の規則に従って動いたときに返す数列が辞書順で最大になるように (1,2,\dots,N) の順列 Q を指定する。
- 木の頂点 1 にいる高橋くんは、まずすぬけくんから Q を受け取り、整数列 R を長さ 1 の列 (Q_1) で初期化する。その後 N-1 回次のように動き、最終的な R を返す。
- 過去に訪れたことのある頂点に隣接しているがまだ訪れていない頂点であって、最も Q_i が小さい頂点 i を訪れる(このような頂点は常にただ 1 つ存在することが証明できる)。その後、R の末尾に Q_i を追加する。
ただし、頂点 1 ははじめに訪れたものとみなします。
高橋くんが P を返すような木が存在するか判定してください。
T 個のテストケースが与えられるので、それぞれについて答えてください。
制約
- 1 \le T \le 10^5
- 1 \le N \le 2 \times 10^5
- P は (1,2,\dots,N) の順列
- 全てのテストケースにおける N の総和は 2 \times 10^5 以下
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N P_1 P_2 \ldots P_N
出力
T 行出力せよ。
i 行目には i 番目のテストケースについて、条件を満たす木が存在するならば Yes 、そうでないならば No を出力せよ。
入力例 1
3 4 4 2 3 1 4 4 1 3 2 11 11 8 9 6 7 10 3 4 5 1 2
出力例 1
Yes No Yes
1 番目のテストケースについて、頂点 1-2 間、1-3 間、2-4 間に辺のある 4 頂点の木が条件を満たします。
すぬけくんが Q=(4,3,2,1) を指定すると、高橋くんは頂点 1,3,2,4 の順に動き、R=(4,2,3,1) が返されます。すぬけくんが Q をどのように指定しても、辞書順で (4,2,3,1) よりも大きい数列が高橋くんから返されるようにはできないので、結局返される数列は (4,2,3,1) です。
2 番目のテストケースについて、条件を満たす木は存在しないことが証明できます。
Score : 800 points
Problem Statement
You are given a positive integer N and a permutation P of (1,2,\dots,N).
When there is a tree with N vertices numbered 1,2,\dots,N, Snuke and Takahashi move according to the following rules:
- Snuke specifies a permutation Q of (1,2,\dots,N) such that the sequence returned by Takahashi, when following the rules below, is lexicographically maximum.
- Takahashi, who is at vertex 1 of the tree, first receives Q from Snuke and initializes an integer sequence R with the length-1 sequence (Q_1). Then, he moves N-1 times as follows, and returns the final R:
- Visit the vertex i with the smallest Q_i among vertices that are adjacent to a previously visited vertex but have not yet been visited (it can be proved that there is always exactly one such vertex). Then, append Q_i to the end of R.
Here, vertex 1 is considered to have been visited initially.
Determine whether there exists a tree such that Takahashi returns P.
You are given T test cases; solve each of them.
Constraints
- 1 \le T \le 10^5
- 1 \le N \le 2 \times 10^5
- P is a permutation of (1,2,\dots,N).
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N P_1 P_2 \ldots P_N
Output
Output T lines.
The i-th line should contain Yes if there exists a tree satisfying the conditions for the i-th test case, and No otherwise.
Sample Input 1
3 4 4 2 3 1 4 4 1 3 2 11 11 8 9 6 7 10 3 4 5 1 2
Sample Output 1
Yes No Yes
For the first test case, a tree with four vertices having edges between vertices 1-2, 1-3, and 2-4 satisfies the conditions.
If Snuke specifies Q=(4,3,2,1), Takahashi visits vertices 1,3,2,4 in this order and returns R=(4,2,3,1). No matter what Q Snuke specifies, it is impossible to make Takahashi return a sequence lexicographically larger than (4,2,3,1), so the sequence that ends up being returned is (4,2,3,1).
For the second test case, it can be proved that there is no tree satisfying the conditions.