A - Lexicographic Order

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

相異なる二つの文字列 S, T が与えられます。
ST よりも辞書順で小さい場合は Yes を、大きい場合は No を出力してください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. ST のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、 ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

なお、主要なプログラミング言語の多くでは、文字列の辞書順による比較は標準ライブラリに含まれる関数や演算子として実装されています。詳しくは各言語のリファレンスをご参照ください。

制約

  • S, T は英小文字からなる長さ 1 以上 10 以下の相異なる文字列である。

入力

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

S T

出力

ST より辞書順で小さい場合は Yes を、大きい場合は No を出力せよ。


入力例 1

abc atcoder

出力例 1

Yes

abcatcoder1 文字目が同じで 2 文字目が異なります。 アルファベットの bt よりもアルファベット順で先に来るので、 abc の方が atcoder よりも辞書順で小さいことがわかります。


入力例 2

arc agc

出力例 2

No

入力例 3

a aa

出力例 3

Yes

Score : 100 points

Problem Statement

You are given two different strings S and T.
If S is lexicographically smaller than T, print Yes; otherwise, print No.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Note that many major programming languages implement lexicographical comparison of strings as operators or functions in standard libraries. For more detail, see your language's reference.

Constraints

  • S and T are different strings, each of which consists of lowercase English letters and has a length of between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

If S is lexicographically smaller than T, print Yes; otherwise, print No.


Sample Input 1

abc atcoder

Sample Output 1

Yes

abc and atcoder begin with the same character, but their second characters are different. Since b comes earlier than t in alphabetical order, we can see that abc is lexicographically smaller than atcoder.


Sample Input 2

arc agc

Sample Output 2

No

Sample Input 3

a aa

Sample Output 3

Yes
B - Buy a Pen

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君はペンを買うためにお店にやって来ました。お店では赤色のペンが一本 R 円、緑色のペンが一本 G 円、青色のペンが一本 B 円で売られています。

高橋君は色 C が嫌いです。CRed のときは赤色のペンを、Green のときは緑色のペンを、Blue のときは青色のペンを買うことができません。

高橋君がペンを一本買うために必要な金額の最小値を求めてください。

制約

  • 1\leq R,G,B\leq 100
  • R,G,B は整数
  • CRed,Green,Blue のいずれか

入力

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

R G B
C

出力

高橋君がペンを一本買うために必要な金額の最小値が X 円であるとき、X を出力せよ。


入力例 1

20 30 10
Blue

出力例 1

20

赤色のペンは 20 円で、緑色のペンは 30 円で、青色のペンは 10 円で売られています。高橋君は青色のペンを買うことができないので、20 円で赤色のペンを一本買うことができます。


入力例 2

100 100 100
Red

出力例 2

100

入力例 3

37 39 93
Blue

出力例 3

37

Score : 100 points

Problem Statement

Takahashi came to a store to buy a pen. Here, a red pen costs R yen, a green pen costs G yen, and a blue pen costs B yen.

Takahashi dislikes the color C. If C is Red, he cannot buy a red pen; if C is Green, he cannot buy a green pen; and if C is Blue, he cannot buy a blue pen.

Determine the minimum amount of money he needs to buy one pen.

Constraints

  • 1\leq R,G,B\leq 100
  • R, G, and B are integers.
  • C is Red, Green, or Blue.

Input

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

R G B
C

Output

If the minimum amount of money Takahashi needs to buy one pen is X yen, print X.


Sample Input 1

20 30 10
Blue

Sample Output 1

20

A red pen costs 20 yen, a green pen costs 30 yen, and a blue pen costs 10 yen. Takahashi cannot buy a blue pen, but he can buy a red pen for 20 yen.


Sample Input 2

100 100 100
Red

Sample Output 2

100

Sample Input 3

37 39 93
Blue

Sample Output 3

37
C - Batters

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は野球をモチーフにしたゲームを作ろうとしましたが、うまくコードが書けなくて困っています。
高橋君の代わりに次の問題を解くプログラムを作ってください。

マス 0, マス 1, マス 2, マス 34 つのマス目があります。はじめマスの上には何もありません。
また、整数 P があり、はじめ P = 0 です。
正の整数からなる数列 A = (A_1, A_2, \dots, A_N) が与えられるので、i = 1, 2, \dots, N について順番に次の操作を行います。

  1. マス 0 に駒を 1 個置く。
  2. マス上のすべての駒を番号が A_i 大きいマスに進める。言い換えると、駒がマス x にあればその駒をマス x + A_i に移動する。
    ただし移動先のマスが存在しない (すなわち x + A_i4 以上になる) 駒たちに関しては、それらを取り除いて P に取り除いた個数を加算する。

すべての操作を行った後の P の値を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 4
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

操作終了時点での P の値を出力せよ。


入力例 1

4
1 1 3 2

出力例 1

3

操作を説明すると次のようになり、操作終了時点での P の値は 3 になります。

  • i=1 での操作
    1. マス 0 に駒を置く。この時点でマス 0 にコマが乗っている。
    2. すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1 に駒が乗っている。
  • i=2 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 1 にコマが乗っている。
    2. すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1, 2 に駒が乗っている。
  • i=3 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 1, 2 にコマが乗っている。
    2. すべての駒を 3 大きいマスに進める。
      この時、マス 1,2 にある駒は移動先のマスが存在しないため (それぞれ 1+3=4,2+3=5 なので) 、盤上から取り除いて P2 を加算する。P の値は 2 になる。
      移動を終えた時点でマス 3 に駒が乗っている。
  • i=4 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 3 にコマが乗っている。
    2. すべての駒を 2 大きいマスに進める。
      この時、マス 3 にある駒は移動先のマスが存在しないため (3+2=5 なので) 、盤上から取り除いて P1 を加算する。P の値は 3 になる。
      移動を終えた時点でマス 2 に駒が乗っている。

入力例 2

3
1 1 1

出力例 2

0

P の値が操作中に変化しない場合もあります。


入力例 3

10
2 2 4 1 1 1 4 2 2 1

出力例 3

8

Score : 200 points

Problem Statement

Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.

There are 4 squares called Square 0, Square 1, Square 2, and Square 3. Initially, all squares are empty.
There is also an integer P; initially, P = 0.
Given a sequence of positive integers A = (A_1, A_2, \dots, A_N), perform the following operations for i = 1, 2, \dots, N in this order:

  1. Put a piece on Square 0.
  2. Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i).
    If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4) for a piece, remove it. Add to P the number of pieces that have been removed.

Print the value of P after all the operations have been performed.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 4
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the value of P after all the operations have been performed.


Sample Input 1

4
1 1 3 2

Sample Output 1

3

The operations are described below. After all the operations have been performed, P equals 3.

  • The operations for i=1:
    1. Put a piece on Square 0. Now, Square 0 has a piece.
    2. Advance every piece on the squares 1 square ahead. After these moves, Square 1 has a piece.
  • The operations for i=2:
    1. Put a piece on Square 0. Now, Squares 0 and 1 have a piece.
    2. Advance every piece on the squares 1 square ahead. After these moves, Squares 1 and 2 have a piece.
  • The operations for i=3:
    1. Put a piece on Square 0. Now, Squares 0, 1, and 2 have a piece.
    2. Advance every piece on the squares 3 squares ahead.
      Here, for the pieces on Squares 1 and 2, the destination squares do not exist (since 1+3=4 and 2+3=5), so remove these pieces and add 2 to P. P now equals 2. After these moves, Square 3 has a piece.
  • The operations for i=4:
    1. Put a piece on Square 0. Now, Squares 0 and 3 have a piece.
    2. Advance every piece on the squares 2 squares ahead.
      Here, for the piece on Square 3, the destination square does not exist (since 3+2=5), so remove this piece and add 1 to P. P now equals 3.
      After these moves, Square 2 has a piece.

Sample Input 2

3
1 1 1

Sample Output 2

0

The value of P may not be updated by the operations.


Sample Input 3

10
2 2 4 1 1 1 4 2 2 1

Sample Output 3

8
D - tcaF

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

2 以上の整数 X が与えられます。

N!=X を満たすような正の整数 N を求めてください。

ただし、N!N の階乗を表し、そのような N がただ一つ存在することは保証されています。

制約

  • 2 \leq X \leq 3 \times 10^{18}
  • N!=X を満たすような正の整数 N がただ一つ存在する
  • 入力は全て整数

入力

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

X

出力

答えを出力せよ。


入力例 1

6

出力例 1

3

3!=3\times2\times1=6 より、3 を出力します。


入力例 2

2432902008176640000

出力例 2

20

20!=2432902008176640000 より、20 を出力します。

Score : 150 points

Problem Statement

You are given an integer X not less than 2.

Find the positive integer N such that N! = X.

Here, N! denotes the factorial of N, and it is guaranteed that there is exactly one such N.

Constraints

  • 2 \leq X \leq 3 \times 10^{18}
  • There is exactly one positive integer N such that N!=X.
  • All input values are integers.

Input

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

X

Output

Print the answer.


Sample Input 1

6

Sample Output 1

3

From 3!=3\times2\times1=6, print 3.


Sample Input 2

2432902008176640000

Sample Output 2

20

From 20!=2432902008176640000, print 20.

E - Graph Isomorphism

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君と青木君は、それぞれ N 個のボールに M 本のひもを取り付けたおもちゃを持っています。

高橋君のおもちゃにおいて、ボールには 1, \dots, N と番号が付けられており、i \, (1 \leq i \leq M) 本目のひもはボール A_i とボール B_i を結んでいます。

青木君のおもちゃにおいても同様に、ボールには 1, \dots, N と番号が付けられており、i \, (1 \leq i \leq M) 本目のひもはボール C_i とボール D_i を結んでいます。

それぞれのおもちゃにおいて、同一のボールを結ぶようなひもは存在せず、2 つのボールを 2 本以上の異なるひもが結んでいることはありません。

すぬけ君は、2 人のおもちゃが同じ形であるかどうか気になっています。
ここで、2 人のおもちゃが同じ形であるとは、以下の条件を満たす数列 P が存在することをいいます。

  • P(1, \dots, N) を並べ替えて得られる。
  • 任意の 1 以上 N 以下の整数 i, j に対し、以下が成り立つ。
    • 高橋君のおもちゃにおいてボール i, j がひもで繋がれていることと、青木君のおもちゃにおいてボール P_i, P_j がひもで繋がれていることは同値である。

2 人のおもちゃが同じ形であるなら Yes、そうでないなら No と出力してください。

制約

  • 1 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (A_i, B_i) \neq (A_j, B_j) \, (i \neq j)
  • 1 \leq C_i \lt D_i \leq N \, (1 \leq i \leq M)
  • (C_i, D_i) \neq (C_j, D_j) \, (i \neq j)
  • 入力は全て整数である。

入力

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

N M
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_M D_M

出力

2 人のおもちゃが同じ形であるなら Yes、そうでないなら No と出力せよ。


入力例 1

4 4
1 2
1 3
1 4
3 4
1 3
1 4
2 3
3 4

出力例 1

Yes

高橋君のおもちゃは下図の左側のような形をしており、青木君のおもちゃは下図の右側のような形をしています。

yes1

次の図から、2 人のおもちゃが同じ形であることがわかります。例えば P = (3, 2, 1, 4) とすると問題文中の条件を満たします。

yes2


入力例 2

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

出力例 2

No

2 人のおもちゃは同じ形ではありません。

no


入力例 3

8 0

出力例 3

Yes

Score : 300 points

Problem Statement

Takahashi and Aoki each have a toy made by attaching M cords to N balls.

In Takahashi's toy, the balls are numbered 1, \dots, N, and the i-th cord ties Ball A_i and B_i.

Similarly, in Aoki's toy, the balls are numbered 1, \dots, N, and the i-th cord ties Ball C_i and D_i.

In each toy, no cord ties a ball to itself, and no two balls are tied by two or more different cords.

Snuke is wondering whether the two toys have the same shape.
Here, they are said to have the same shape when there is a sequence P that satisfies the conditions below.

  • P is a permutation of (1, \dots, N).
  • For every pair of integers i, j between 1 and N (inclusive), the following holds.
    • Balls i and j in Takahashi's toy are tied by a cord if and only if Balls P_i and P_j in Aoki's toy are tied by a cord.

If the two toys have the same shape, print Yes; otherwise, print No.

Constraints

  • 1 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (A_i, B_i) \neq (A_j, B_j) \, (i \neq j)
  • 1 \leq C_i \lt D_i \leq N \, (1 \leq i \leq M)
  • (C_i, D_i) \neq (C_j, D_j) \, (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_M D_M

Output

If the two toys have the same shape, print Yes; otherwise, print No.


Sample Input 1

4 4
1 2
1 3
1 4
3 4
1 3
1 4
2 3
3 4

Sample Output 1

Yes

Takahashi's toy is illustrated on the left in the figure below, and Aoki's is illustrated on the right.

yes1

The following figure shows that the two toys have the same shape. The condition in the statement is satisfied when P = (3, 2, 1, 4), for example.

yes2


Sample Input 2

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

Sample Output 2

No

The two toys do not have the same shape.

no


Sample Input 3

8 0

Sample Output 3

Yes
F - Colorful Beans

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

N 種類のビーンズが 1 粒ずつあります。 i 種類目のビーンズはおいしさが A_i で色が C_i です。ビーンズは混ぜられており、色でしか区別することができません。

あなたはビーンズの色を 1 つ選び、その色のビーンズをどれか 1 粒食べます。ビーンズの色をうまく選ぶことで、食べる可能性のあるビーンズのおいしさの最小値を最大化してください。

制約

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq A_i \leq 10^{9}
  • 1 \leq C_i \leq 10^{9}
  • 入力は全て整数である。

入力

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

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

出力

食べる可能性のあるビーンズのおいしさの最小値の最大値を整数として出力せよ。


入力例 1

4
100 1
20 5
30 5
40 1

出力例 1

40

同じ色のビーンズは互いに区別がつかないことに注意してください。

選ぶことができる色は 色 1 と 色 5 です。

  • 1 のビーンズは 2 種類あり、美味しさはそれぞれ 100, 40 です。よって、色 1 を選んだときのおいしさの最小値は 40 です。
  • 5 のビーンズは 2 種類あり、美味しさはそれぞれ 20, 30 です。よって、色 5 を選んだときのおいしさの最小値は 20 です。

おいしさの最小値を最大化するには 色 1 を選べばよいため、そのときの最小値である 40 を出力します。


入力例 2

10
68 3
17 2
99 2
92 4
82 4
10 3
100 2
78 1
3 1
35 4

出力例 2

35

Score: 250 points

Problem Statement

There are N types of beans, one bean of each type. The i-th type of bean has a deliciousness of A_i and a color of C_i. The beans are mixed and can only be distinguished by color.

You will choose one color of beans and eat one bean of that color. By selecting the optimal color, maximize the minimum possible deliciousness of the bean you eat.

Constraints

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq A_i \leq 10^{9}
  • 1 \leq C_i \leq 10^{9}
  • All input values are integers.

Input

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

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

Output

Print as an integer the maximum value of the minimum possible deliciousness of the bean you eat.


Sample Input 1

4
100 1
20 5
30 5
40 1

Sample Output 1

40

Note that beans of the same color cannot be distinguished from each other.

You can choose color 1 or color 5.

  • There are two types of beans of color 1, with deliciousness of 100 and 40. Thus, the minimum deliciousness when choosing color 1 is 40.
  • There are two types of beans of color 5, with deliciousness of 20 and 30. Thus, the minimum deliciousness when choosing color 5 is 20.

To maximize the minimum deliciousness, you should choose color 1, so print the minimum deliciousness in that case: 40.


Sample Input 2

10
68 3
17 2
99 2
92 4
82 4
10 3
100 2
78 1
3 1
35 4

Sample Output 2

35
G - Peaceful Teams

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N 人のスポーツ選手がいます。

N 人の選手たちには互いに相性の悪い選手のペアが M 組あり、相性の悪い組のうち i\ (1\leq i\leq M) 組目は A _ i 番目の選手と B _ i 番目の選手です。

あなたは、選手を T チームに分けます。 どの選手もちょうど一つのチームに属さなければならず、どのチームにも少なくとも一人の選手が属さなければなりません。 さらに、どの i=1,2,\ldots,M についても、 A _ i 番目の選手と B _ i 番目の選手が同じチームに属していてはいけません。

この条件を満たすチーム分けの方法は何通りあるか求めてください。 ただし、チーム分けの方法が異なるとは、ある二人が存在して、彼らが一方のチーム分けでは同じチームに所属し、もう一方では異なるチームに所属することをいいます。

制約

  • 1\leq T\leq N\leq10
  • 0\leq M\leq\dfrac{N(N-1)}2
  • 1\leq A _ i\lt B _ i\leq N\ (1\leq i\leq M)
  • (A _ i,B _ i)\neq (A _ j,B _ j)\ (1\leq i\lt j\leq M)
  • 入力はすべて整数

入力

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

N T M
A _ 1 B _ 1
A _ 2 B _ 2
\vdots
A _ M B _ M

出力

答えを 1 行で出力せよ。


入力例 1

5 2 2
1 3
3 4

出力例 1

4

次の 4 通りのチーム分けが条件を満たします。

他に条件を満たすチーム分けは存在しないので、4 を出力してください。


入力例 2

5 1 2
1 3
3 4

出力例 2

0

条件を満たすチーム分けがひとつも存在しないこともあります。


入力例 3

6 4 0

出力例 3

65

相性の悪いペアがひとつも存在しないこともあります。


入力例 4

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

出力例 4

8001

Score : 400 points

Problem Statement

There are N sports players.

Among them, there are M incompatible pairs. The i-th incompatible pair (1\leq i\leq M) is the A_i-th and B_i-th players.

You will divide the players into T teams. Every player must belong to exactly one team, and every team must have one or more players. Additionally, for each i=1,2,\ldots,M, the A_i-th and B_i-th players must not belong to the same team.

Find the number of ways to satisfy these conditions. Here, two divisions are considered different when there are two players who belong to the same team in one division and different teams in the other.

Constraints

  • 1\leq T\leq N\leq10
  • 0\leq M\leq\dfrac{N(N-1)}2
  • 1\leq A _ i\lt B _ i\leq N\ (1\leq i\leq M)
  • (A _ i,B _ i)\neq (A _ j,B _ j)\ (1\leq i\lt j\leq M)
  • All input values are integers.

Input

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

N T M
A _ 1 B _ 1
A _ 2 B _ 2
\vdots
A _ M B _ M

Output

Print the answer in a single line.


Sample Input 1

5 2 2
1 3
3 4

Sample Output 1

4

The following four divisions satisfy the conditions.

No other division satisfies them, so print 4.


Sample Input 2

5 1 2
1 3
3 4

Sample Output 2

0

There may be no division that satisfies the conditions.


Sample Input 3

6 4 0

Sample Output 3

65

There may be no incompatible pair.


Sample Input 4

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

Sample Output 4

8001
H - ブレスレット

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

アイドルグループである Bit♡Beat のライブでは、 N 人のメンバーそれぞれのブレスレットが周期的に光りますが、メンバーごとに少しずつスタートがずれた状態から始まります。具体的には、 i 人目のメンバーのブレスレットはライブ開始から A_i 秒後に光り、そこから B_i 秒ごとに光ります。

N 人全員のブレスレットが同時に光ることがあるか判定してください。

より厳密には、ライブ開始から x 秒後に全員のブレスレットが光るような非負整数 x が存在するか判定してください。

制約

  • 2\le N\le 2\times 10^5
  • 0\le A_i < B_i \le 10^6
  • 入力される値は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

N 人全員のブレスレットが同時に光ることがあるならば Yes を、ないならば No を出力せよ。


入力例 1

3
1 3
1 12
3 5

出力例 1

Yes

ライブ開始から 13 秒後に全員のブレスレットが同時に光ります。したがって、 Yes を出力してください。


入力例 2

2
0 6
5 6

出力例 2

No

全員のブレスレットが同時に光ることはありません。したがって、No を出力してください。


入力例 3

10
23192 199879
152914 434323
486127 642530
867889 926999
362331 897721
75620 913403
100107 117031
22740 516557
260622 642909
419828 739597

出力例 3

Yes

ライブ開始から 382791477 秒後に全員のブレスレットが同時に光ります。

I - Swap and Sort

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

(1,2,\ldots,N) を並び替えた長さ N の順列 P=(P_1,P_2,\ldots,P_N) があります。

あなたが可能な操作は M 種類あり、操作 i は「 Pa_i 番目の要素と Pb_i 番目の要素を入れ替える」というものです。

操作を好きな順序で、合計 5\times 10^5 回以下行うことによって、P を昇順に並び替えることはできますか?

できるならば、操作手順を 1 つ教えてください。できないならばその旨を伝えてください。

制約

  • 2\leq N \leq 1000
  • P(1,2,\ldots,N) を並び替えた順列
  • 1\leq M \leq \min(2\times 10^5, \frac{N(N-1)}{2})
  • 1\leq a_i \lt b_i\leq N
  • i\neq j ならば (a_i,b_i)\neq (a_j,b_j)
  • 入力に含まれる値は全て整数

入力

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

N
P_1 P_2 \ldots P_N
M
a_1 b_1
a_2 b_2
\vdots
a_M b_M

出力

P を昇順に並び替えることができるならば以下の形式で出力せよ。

K
c_1 c_2 \ldots c_K

ここで、K は操作の回数を表し、c_i(1\leq i \leq K)i 回目に行う操作が操作 c_i であることを表す。
0\leq K \leq 5\times 10^5 を満たさなければならないことに注意せよ。

P を昇順に並び替えることができないならば、-1 と出力せよ。


入力例 1

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

出力例 1

3
4 2 1

P は、(5,3,2,4,6,1)\to (5,2,3,4,6,1)\to (5,2,3,4,1,6)\to (1,2,3,4,5,6) と変化します。


入力例 2

5
3 4 1 2 5
2
1 3
2 5

出力例 2

-1

P を昇順に並び替えることはできません。


入力例 3

4
1 2 3 4
6
1 2
1 3
1 4
2 3
2 4
3 4

出力例 3

0

初めから P が昇順に並んでいることもあります。

また、以下のような答えも正解になります。

4
5 5 5 5

操作の回数を最小化する必要はないことに注意してください。

Score : 500 points

Problem Statement

We have a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N).

There are M kinds of operations available to you. Operation i swaps the a_i-th and b_i-th elements of P.

Is it possible to sort P in ascending order by doing at most 5\times 10^5 operations in total in any order?

If it is possible, give one such sequence of operations. Otherwise, report so.

Constraints

  • 2\leq N \leq 1000
  • P is a permutation of (1,2,\ldots,N).
  • 1\leq M \leq \min(2\times 10^5, \frac{N(N-1)}{2})
  • 1\leq a_i \lt b_i\leq N
  • (a_i,b_i)\neq (a_j,b_j) if i\neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N
M
a_1 b_1
a_2 b_2
\vdots
a_M b_M

Output

If it is possible to sort P in ascending order, print the following:

K
c_1 c_2 \ldots c_K

Here, K represents the number of operations to do, and c_i (1\leq i \leq K) means the i-th operation to do is Operation c_i.
Note that 0\leq K \leq 5\times 10^5 must hold.

If it is impossible to sort P in ascending order, print -1.


Sample Input 1

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

Sample Output 1

3
4 2 1

P changes as follows: (5,3,2,4,6,1)\to (5,2,3,4,6,1)\to (5,2,3,4,1,6)\to (1,2,3,4,5,6).


Sample Input 2

5
3 4 1 2 5
2
1 3
2 5

Sample Output 2

-1

We cannot sort P in ascending order.


Sample Input 3

4
1 2 3 4
6
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output 3

0

P may already be sorted in ascending order.

Additionally, here is another accepted output:

4
5 5 5 5

Note that it is not required to minimize the number of operations.