A - Tree Contraction Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700 点

問題文

N 頂点からなる根付き木があります. 頂点には 1,2,\ldots,N の番号がついており,根は頂点 1 です. 各頂点 v (2 \leq v \leq N) の親は頂点 P_v であり,1 \leq P_v < v を満たします.

また,各頂点は整数列を持っています. 最初,頂点 v の持つ列は (v) です.

これから次の操作を N-1 回行います.

  • 2 \leq v \leq N を満たす頂点のうち,これまでの操作で選んでいない頂点 v を選び,現在の木におけるその親を u とする.
  • 頂点 u の列の末尾に,頂点 v の列を連結する.
  • 頂点 v をその親 u に縮約する.すなわち,頂点 v を削除し,その子をすべて頂点 u の子にする.

すべての操作を終えたとき,残った頂点 1 の列が (1,2,\ldots,N) と一致する操作列の個数を,998244353 で割った余りを求めてください.ここで,各回で選ぶ頂点の列が異なる操作列は異なるものとして数えます.

1 つの入力につき,T ケースを解いてください.

制約

  • 1 \leq T \leq 125000
  • 2 \leq N \leq 250000
  • 1 \leq P_v < v (2 \leq v \leq N)
  • T ケースにわたる N の総和は 250000 以下である.
  • 入力される数値はすべて整数である.

入力

入力は標準入力から以下の形式で与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N
P_2 P_3 \ldots P_N

出力

各テストケースについて,答えを出力せよ.


入力例 1

6
4
1 2 1
5
1 1 3 3
2
1
5
1 2 3 4
4
1 1 1
8
1 1 2 4 2 6 3

出力例 1

2
6
1
24
1
24

1 番目のテストケースでは,例えば頂点を 3,2,4 の順に選ぶと,次のように列が変化します.

  • 頂点 3 を選ぶと,頂点 2 の列は (2,3) になる.
  • 頂点 2 を選ぶと,頂点 1 の列は (1,2,3) になる.
  • 頂点 4 を選ぶと,頂点 1 の列は (1,2,3,4) になる.

条件を満たす頂点の選択順は (2,3,4) と (3,2,4) の 2 通りです.

Score : 700 points

Problem Statement

There is a rooted tree with N vertices. The vertices are numbered 1,2,\ldots,N, and the root is vertex 1. The parent of each vertex v (2 \leq v \leq N) is vertex P_v, where 1 \leq P_v < v.

Additionally, each vertex holds an integer sequence. Initially, the sequence held by vertex v is (v).

You will perform the following operation N-1 times.

  • Choose a vertex v with 2 \leq v \leq N that has not been chosen in any previous operation, and let u be its parent in the current tree.
  • Concatenate the sequence of vertex v to the end of the sequence of vertex u.
  • Contract vertex v into its parent u. That is, delete vertex v and make all of its children into children of vertex u.

Find the number, modulo 998244353, of operation sequences such that the sequence of the remaining vertex 1 after all operations equals (1,2,\ldots,N). Here, two operation sequences are considered different if their sequences of chosen vertices are different.

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 125000
  • 2 \leq N \leq 250000
  • 1 \leq P_v < v (2 \leq v \leq N)
  • The sum of N over all test cases in a single input is at most 250000.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
P_2 P_3 \ldots P_N

Output

For each test case, output the answer.


Sample Input 1

6
4
1 2 1
5
1 1 3 3
2
1
5
1 2 3 4
4
1 1 1
8
1 1 2 4 2 6 3

Sample Output 1

2
6
1
24
1
24

In the first test case, if the vertices are chosen in the order 3,2,4, for example, the sequences change as follows.

  • Choosing vertex 3 makes the sequence of vertex 2 equal to (2,3).
  • Choosing vertex 2 makes the sequence of vertex 1 equal to (1,2,3).
  • Choosing vertex 4 makes the sequence of vertex 1 equal to (1,2,3,4).

There are 2 orders of choosing vertices that satisfy the condition: (2,3,4) and (3,2,4).

B - Range Record

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 1100 点

問題文

整数 N,M と,整数の 3 つ組 (L_i,R_i,C_i) が M 組与えられます.ここで,1 \leq L_i < R_i \leq N であり,組 (L_i,R_i) は互いに異なります.

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) であって,次の M 個の条件をすべて満たすものが存在するか判定してください.また存在する場合は,そのような順列を 1 つ求めてください.

  • i 番目の条件: L_i \leq j \leq R_i かつ \max(P_{L_i},P_{L_i+1},\ldots,P_j)=P_j を満たす整数 j の個数が,ちょうど C_i 個である.

1 つの入力につき,T ケースを解いてください.

制約

  • 1 \leq T \leq 125000
  • 2 \leq N \leq 100
  • 1 \leq M \leq \dfrac{N(N-1)}{2}
  • 1 \leq L_i \lt R_i \leq N (1 \leq i \leq M)
  • (L_i,R_i) \neq (L_j,R_j) (1 \leq i \lt j \leq M)
  • 1 \leq C_i \leq R_i-L_i+1 (1 \leq i \leq M)
  • T ケースにわたる N^3 の総和は 10^6 以下である.
  • 入力される数値はすべて整数である.

入力

入力は標準入力から以下の形式で与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N M
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_M R_M C_M

出力

各テストケースについて,条件を満たす順列が存在する場合は,1 行目に Yes を出力し,2 行目に条件を満たす順列 P を空白区切りで出力せよ.条件を満たす順列が複数存在する場合は,どれを出力してもよい.

存在しない場合は No を 1 行に出力せよ.


入力例 1

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

出力例 1

Yes
2 4 1 3
No
Yes
2 1
Yes
1 2
Yes
2 1 4 6 3 5
Yes
2 5 1 4 3
No
Yes
2 1 3

1 番目のテストケースでは,例えば P=(2,4,1,3) が条件を満たします. 区間 [1,4] では j=1,2,区間 [2,4] では j=2,区間 [3,4] では j=3,4 がそれぞれ条件を満たします.

2 番目のテストケースでは,最初の 2 つの条件から P_1 \lt P_2 \lt P_3 が必要です.このとき区間 [1,3] で条件を満たす j の個数は 3 個となり,3 つ目の条件を満たさないので,そのような順列は存在しません.

Score : 1100 points

Problem Statement

You are given integers N,M and M triples of integers (L_i,R_i,C_i). Here, 1 \leq L_i < R_i \leq N, and the pairs (L_i,R_i) are pairwise distinct.

Determine whether there exists a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) that satisfies all of the following M conditions. If one exists, find one such permutation.

  • The i-th condition: the number of integers j such that L_i \leq j \leq R_i and \max(P_{L_i},P_{L_i+1},\ldots,P_j)=P_j is exactly C_i.

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 125000
  • 2 \leq N \leq 100
  • 1 \leq M \leq \dfrac{N(N-1)}{2}
  • 1 \leq L_i \lt R_i \leq N (1 \leq i \leq M)
  • (L_i,R_i) \neq (L_j,R_j) (1 \leq i \lt j \leq M)
  • 1 \leq C_i \leq R_i-L_i+1 (1 \leq i \leq M)
  • The sum of N^3 over all test cases in a single input is at most 10^6.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N M
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_M R_M C_M

Output

For each test case, if a permutation satisfying the conditions exists, output Yes on the first line and the elements of such a permutation P, separated by spaces, on the second line. If there are multiple such permutations, any of them will be accepted.

If no such permutation exists, output No on a single line.


Sample Input 1

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

Sample Output 1

Yes
2 4 1 3
No
Yes
2 1
Yes
1 2
Yes
2 1 4 6 3 5
Yes
2 5 1 4 3
No
Yes
2 1 3

In the first test case, for example, P=(2,4,1,3) satisfies the conditions. The integers j counted in each condition are j=1,2 for the interval [1,4], j=2 for the interval [2,4], and j=3,4 for the interval [3,4].

In the second test case, the first two conditions require P_1 \lt P_2 \lt P_3. Then, the number of integers j counted for the interval [1,3] is 3, which violates the third condition, so no such permutation exists.

C - Sort by Queue and Reverse

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 1100 点

問題文

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) が与えられます.また,最初は空のキューがあります.

次の 3 種類の操作を,好きな順序で何回でも行えます.

  • 追加操作: P が空でない場合,P の先頭の要素を取り除き,その要素をキューの末尾に追加する.
  • 出力操作: キューが空でない場合,先頭の要素を取り除き,その値を出力する.
  • 反転操作: キュー内の要素の順序を反転する.つまり,現在のキューの要素が (v_1,v_2,\ldots,v_k) だった場合,これを (v_k,v_{k-1},\ldots,v_1) に置き換える.

すべての要素を出力し,出力した値が順に 1,2,\ldots,N となるようにしたいです.

これを実現することが可能かどうか判定してください.また可能な場合は,必要な反転操作の最小回数を求めてください.

1 つの入力につき,T ケースを解いてください.

制約

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • 1 \leq P_i \leq N (1 \leq i \leq N)
  • P_1,P_2,\ldots,P_N は互いに異なる.
  • T ケースにわたる N の総和は 250000 以下である.
  • 入力される数値はすべて整数である.

入力

入力は標準入力から以下の形式で与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N
P_1 P_2 \ldots P_N

出力

各テストケースについて,出力した値を順に 1,2,\ldots,N とすることが可能ならば必要な反転操作の最小回数を,不可能ならば -1 を出力せよ.


入力例 1

10
4
3 2 4 1
5
2 4 3 5 1
1
1
4
1 2 3 4
5
5 4 3 2 1
8
2 1 4 3 6 5 8 7
2
2 1
1
1
9
2 8 3 5 6 7 1 9 4
3
2 3 1

出力例 1

3
-1
0
0
1
4
1
0
-1
2

1 番目のテストケースでは,例えば次のように操作できます.

操作 操作後のキュー ここまでに出力した値
追加操作 \times 2 (3,2) なし
反転操作 (2,3) なし
追加操作 \times 2 (2,3,4,1) なし
反転操作 (1,4,3,2) なし
出力操作 (4,3,2) 1
反転操作 (2,3,4) 1
出力操作 \times 3 空 1,2,3,4

反転操作を 3 回行っています.2 回以下の反転操作で実現することはできないため,答えは 3 です.

Score : 1100 points

Problem Statement

You are given a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N). Additionally, there is a queue that is initially empty.

You can perform the following three types of operations any number of times, in any order.

  • Append operation: If P is not empty, remove the first element of P and add that element to the end of the queue.
  • Output operation: If the queue is not empty, remove the front element and output its value.
  • Reverse operation: Reverse the order of the elements in the queue. That is, if the elements of the queue are currently (v_1,v_2,\ldots,v_k), replace them with (v_k,v_{k-1},\ldots,v_1).

You want to output all elements so that the output values are 1,2,\ldots,N in this order.

Determine whether this is possible. If it is possible, find the minimum number of reverse operations required.

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • 1 \leq P_i \leq N (1 \leq i \leq N)
  • P_1,P_2,\ldots,P_N are pairwise distinct.
  • The sum of N over all test cases in a single input is at most 250000.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
P_1 P_2 \ldots P_N

Output

For each test case, output the minimum number of reverse operations required if it is possible to make the output values 1,2,\ldots,N in this order, and -1 otherwise.


Sample Input 1

10
4
3 2 4 1
5
2 4 3 5 1
1
1
4
1 2 3 4
5
5 4 3 2 1
8
2 1 4 3 6 5 8 7
2
2 1
1
1
9
2 8 3 5 6 7 1 9 4
3
2 3 1

Sample Output 1

3
-1
0
0
1
4
1
0
-1
2

In the first test case, for example, you can perform the operations as follows.

Operation Queue after the operation Values output so far
Append operation \times 2 (3,2) None
Reverse operation (2,3) None
Append operation \times 2 (2,3,4,1) None
Reverse operation (1,4,3,2) None
Output operation (4,3,2) 1
Reverse operation (2,3,4) 1
Output operation \times 3 Empty 1,2,3,4

This performs the reverse operation 3 times. It cannot be achieved with 2 or fewer reverse operations, so the answer is 3.

D - Yet Another Degree Sequence Problem

Time Limit: 8 sec / Memory Limit: 1024 MiB

配点 : 1900 点

問題文

次の判定問題 X を考えます.

  • 頂点集合が V=\{1,2,\ldots,N\} である単純無向グラフ G と,長さ N の正整数列 D=(D_1,D_2,\ldots,D_N) が与えられる.ここで,G における頂点 i の次数を \deg_G(i) とすると,D_i\leq \deg_G(i) がすべての頂点 i について成り立つ.また,\sum_{i=1}^N D_i は偶数である.
  • G の辺を 0 本以上削除し,すべての頂点 i の次数を D_i にできるか判定する.できる場合の答えを Possible,できない場合の答えを Impossible とする.

S\subseteq V に対して,次のように定義します.

  • e_G(S):両端がともに S に含まれる G の辺の本数.
  • e_G(i,S):頂点 i\notin S と S 内の頂点を結ぶ G の辺の本数.

すぬけ君は,問題 X について次の予想 Y を考えました.

「V の任意の部分集合 S に対して \left(\sum_{i\in S}D_i\right)-2e_G(S)\leq \sum_{i\notin S}\min(D_i,e_G(i,S)) が成り立つ」ことと,「問題 X の答えが Possible である」ことは同値である.

直感的に言えば,「S から外へ出るべき辺の数」が「S 以外の頂点から S へ向かって張れる辺の個数の最大値の和」を超えてはならない,という条件です.

しかし,この予想は正しくありません.

長さ N の正整数列 D が与えられます.\sum_{i=1}^N D_i は偶数です. 頂点集合が V であり,すべての頂点 i について D_i\leq\deg_G(i) を満たす単純無向グラフ G のうち,(G,D) が予想 Y の反例となるものが存在するか判定してください.

1 つの入力につき,T ケースを解いてください.

制約

  • 1\leq T\leq 125000
  • 2\leq N\leq 250000
  • 1\leq D_i\leq N-1 (1\leq i\leq N)
  • \sum_{i=1}^N D_i は偶数である.
  • T ケースにわたる N の総和は 250000 以下である.
  • 入力される数値はすべて整数である.

入力

入力は標準入力から以下の形式で与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N
D_1 D_2 \ldots D_N

出力

各テストケースについて,予想 Y の反例となるグラフ G が存在する場合は Yes,存在しない場合は No を出力せよ.


入力例 1

8
6
1 1 1 1 1 1
6
2 2 2 2 2 2
2
1 1
7
1 1 1 1 1 1 2
7
2 2 2 2 2 2 6
8
2 2 2 2 2 2 2 2
9
2 2 2 2 2 2 2 2 2
10
2 2 3 3 3 3 3 3 9 9

出力例 1

Yes
No
No
Yes
Yes
No
Yes
Yes

1 番目のテストケースでは,頂点 1,2,3 からなるサイクルと頂点 4,5,6 からなるサイクルを合わせたグラフを G とすればよいです. このとき,例えば S=\{1,2,4\} とすると,

  • 左辺: \left(\sum_{i\in S}D_i\right)-2e_G(S) = (D_1+D_2+D_4)-2 \times 1 = 1
  • 右辺: \sum_{i\notin S}\min(D_i,e_G(i,S)) = \min(D_3,2)+\min(D_5,1)+\min(D_6,1)=3

であり,予想 Y の不等式は満たされています. 同様にして,任意の部分集合 S について条件の成立が確認できます.

一方,G の辺をどのように消したとしても,すべての頂点の次数を 1 にすることはできません.したがってこの G は反例であり,答えは Yes です.

Score : 1900 points

Problem Statement

Consider the following decision problem X.

  • You are given a simple undirected graph G with vertex set V=\{1,2,\ldots,N\} and a length-N sequence of positive integers D=(D_1,D_2,\ldots,D_N). Here, letting \deg_G(i) denote the degree of vertex i in G, the inequality D_i\leq \deg_G(i) holds for every vertex i. Also, \sum_{i=1}^N D_i is even.
  • Determine whether it is possible to delete zero or more edges of G so that the degree of every vertex i becomes D_i. The answer is Possible if this can be done, and Impossible otherwise.

For S\subseteq V, define the following.

  • e_G(S): The number of edges of G with both endpoints in S.
  • e_G(i,S): The number of edges of G connecting vertex i\notin S to a vertex in S.

Snuke came up with the following conjecture Y about problem X.

The statement "for every subset S of V, \left(\sum_{i\in S}D_i\right)-2e_G(S)\leq \sum_{i\notin S}\min(D_i,e_G(i,S)) holds" is equivalent to the statement "the answer to problem X is Possible".

Intuitively, the condition says that "the number of edges that must leave S" must not exceed "the sum, over the vertices outside S, of the maximum number of edges that each of them can have toward S".

However, this conjecture is false.

You are given a length-N sequence of positive integers D, where \sum_{i=1}^N D_i is even. Determine whether there exists a simple undirected graph G with vertex set V satisfying D_i\leq\deg_G(i) for every vertex i such that (G,D) is a counterexample to conjecture Y.

You are given T test cases; solve each of them.

Constraints

  • 1\leq T\leq 125000
  • 2\leq N\leq 250000
  • 1\leq D_i\leq N-1 (1\leq i\leq N)
  • \sum_{i=1}^N D_i is even.
  • The sum of N over all test cases in a single input is at most 250000.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
D_1 D_2 \ldots D_N

Output

For each test case, output Yes if there exists a graph G that is a counterexample to conjecture Y, and No otherwise.


Sample Input 1

8
6
1 1 1 1 1 1
6
2 2 2 2 2 2
2
1 1
7
1 1 1 1 1 1 2
7
2 2 2 2 2 2 6
8
2 2 2 2 2 2 2 2
9
2 2 2 2 2 2 2 2 2
10
2 2 3 3 3 3 3 3 9 9

Sample Output 1

Yes
No
No
Yes
Yes
No
Yes
Yes

In the first test case, you can take G to be the graph consisting of a cycle on vertices 1,2,3 and a cycle on vertices 4,5,6. In this case, for example, for S=\{1,2,4\}:

  • Left-hand side: \left(\sum_{i\in S}D_i\right)-2e_G(S) = (D_1+D_2+D_4)-2 \times 1 = 1
  • Right-hand side: \sum_{i\notin S}\min(D_i,e_G(i,S)) = \min(D_3,2)+\min(D_5,1)+\min(D_6,1)=3

Thus, the inequality in conjecture Y holds. Similarly, the condition can be verified for every subset S.

On the other hand, no matter how the edges of G are deleted, it is impossible to make every vertex have degree 1. Therefore, this G is a counterexample, and the answer is Yes.