A - aaaadaa

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の英小文字からなる文字列 S と英小文字 c_1,c_2 が与えられます。

S の文字のうち c_1 であるもの 以外 を全て c_2 に置き換えた文字列を求めてください。

制約

  • 1\le N\le 100
  • N は整数
  • c_1,c_2 は英小文字
  • S は英小文字からなる長さ N の文字列

入力

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

N c_1 c_2
S

出力

答えを出力せよ。


入力例 1

3 b g
abc

出力例 1

gbg

S= abc のうち、 b でない acg に置き換えた結果 gbg になります。したがって、 gbg を出力してください。


入力例 2

1 s h
s

出力例 2

s

置き換えた後の文字列が元の文字列と変わらない場合もあります。


入力例 3

7 d a
atcoder

出力例 3

aaaadaa

入力例 4

10 b a
acaabcabba

出力例 4

aaaabaabba

Score : 100 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, along with lowercase English letters c_1 and c_2.

Find the string obtained by replacing every character of S that is not c_1 with c_2.

Constraints

  • 1\le N\le 100
  • N is an integer.
  • c_1 and c_2 are lowercase English letters.
  • S is a string of length N consisting of lowercase English letters.

Input

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

N c_1 c_2
S

Output

Print the answer.


Sample Input 1

3 b g
abc

Sample Output 1

gbg

Replacing a and c (which are not b) with g in S= abc results in gbg, so print gbg.


Sample Input 2

1 s h
s

Sample Output 2

s

It is possible that the resulting string after replacement is the same as the original string.


Sample Input 3

7 d a
atcoder

Sample Output 3

aaaadaa

Sample Input 4

10 b a
acaabcabba

Sample Output 4

aaaabaabba
B - Sigma Cubes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

正整数 N が与えられます。

i=1,2,\ldots,N について (-1)^i \times i^3 を計算したときの、それら N 個の値の総和を求めてください。

すなわち、 \displaystyle \sum_{i=1}^N (-1)^i \times i^3 を求めてください。

制約

  • N1 以上 10 以下の整数

入力

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

N

出力

\displaystyle \sum_{i=1}^N (-1)^i \times i^3 の値を出力せよ。


入力例 1

3

出力例 1

-20
  • i=1 のとき: (-1)^i\times i^3=-1 です。
  • i=2 のとき: (-1)^i\times i^3=8 です。
  • i=3 のとき: (-1)^i\times i^3=-27 です。

したがって、 -1 + 8 - 27 = -20 を出力してください。


入力例 2

9

出力例 2

-425

入力例 3

10

出力例 3

575

Score : 100 points

Problem Statement

You are given a positive integer N.

For i=1,2,\ldots,N, calculate (-1)^i \times i^3, and find the sum of these N values.

That is, find \displaystyle \sum_{i=1}^N (-1)^i \times i^3.

Constraints

  • N is an integer between 1 and 10, inclusive.

Input

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

N

Output

Output the value of \displaystyle \sum_{i=1}^N (-1)^i \times i^3.


Sample Input 1

3

Sample Output 1

-20
  • When i=1: (-1)^i\times i^3=-1.
  • When i=2: (-1)^i\times i^3=8.
  • When i=3: (-1)^i\times i^3=-27.

Therefore, output -1 + 8 - 27 = -20.


Sample Input 2

9

Sample Output 2

-425

Sample Input 3

10

Sample Output 3

575
C - Permute to Minimize

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

正整数 X が与えられます。

X を(先頭に 0 を含まない形で)十進表記した際に現れる数字を、先頭に 0 が来ないように 並び替えることで得られる正整数のうち、値が最小のものを求めてください。

制約

  • 1\leq X < 10^5
  • X は整数

入力

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

X

出力

答えを出力せよ。


入力例 1

903

出力例 1

309

X を十進表記した際に現れる数字を先頭に 0 が来ないように並び替えることで得られる正整数は、903, 930, 309, 3904 通りであり、このうち値が最小のものは 309 です。


入力例 2

432

出力例 2

234

入力例 3

100

出力例 3

100

Score : 200 points

Problem Statement

You are given a positive integer X.

Find the minimum value among all positive integers that can be obtained by rearranging the digits appearing in the decimal representation of X (without leading zeros) such that there is no leading zero.

Constraints

  • 1\leq X < 10^5
  • X is an integer.

Input

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

X

Output

Output the answer.


Sample Input 1

903

Sample Output 1

309

There are four positive integers that can be obtained by rearranging the digits appearing in the decimal representation of X such that there is no leading zero: 903, 930, 309, 390; the minimum value among them is 309.


Sample Input 2

432

Sample Output 2

234

Sample Input 3

100

Sample Output 3

100
D - Coloring Matrix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

各要素が 0 あるいは 1 である NN 列の行列 A, B が与えられます。
Ai 行目 j 列目の要素を A_{i,j}Bi 行目 j 列目の要素を B_{i,j} で表します。
A を適切に回転することで、 A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできるか判定してください。
ただし、A を回転するとは、以下の操作を好きな回数(0 回でもよい)繰り返すことをいいます。

  • 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について同時に A_{i,j}A_{N + 1 - j,i} で置き換える

制約

  • 1 \leq N \leq 100
  • A, B の各要素は 01 のいずれか
  • 入力はすべて整数

入力

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

出力

A を適切に回転することで、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできる場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

出力例 1

Yes

はじめ、A

0 1 1
1 0 0
0 1 0

です。
1 回操作を行うと、A

0 1 0
1 0 1 
0 0 1

となります。
もう 1 度操作を行うと、A

0 1 0
0 0 1
1 1 0

となります。
このとき、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているので、Yes を出力します。


入力例 2

2
0 0
0 0
1 1
1 1

出力例 2

Yes

入力例 3

5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0

出力例 3

No

Score : 200 points

Problem Statement

You are given N-by-N matrices A and B where each element is 0 or 1.
Let A_{i,j} and B_{i,j} denote the element at the i-th row and j-th column of A and B, respectively.
Determine whether it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1.
Here, to rotate A is to perform the following operation zero or more times:

  • for every pair of integers (i, j) such that 1 \leq i, j \leq N, simultaneously replace A_{i,j} with A_{N + 1 - j,i}.

Constraints

  • 1 \leq N \leq 100
  • Each element of A and B is 0 or 1.
  • All values in the input are integers.

Input

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

Output

If it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, print Yes; otherwise, print No.


Sample Input 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

Sample Output 1

Yes

Initially, A is :

0 1 1
1 0 0
0 1 0

After performing the operation once, A is :

0 1 0
1 0 1 
0 0 1

After performing the operation once again, A is :

0 1 0
0 0 1
1 1 0

Here, B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, so you should print Yes.


Sample Input 2

2
0 0
0 0
1 1
1 1

Sample Output 2

Yes

Sample Input 3

5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0

Sample Output 3

No
E - Circular Playlist

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 曲からなるプレイリストがあり、曲には 1, \dots, N の番号が付けられています。
i の長さは A_i 秒です。

プレイリストを再生すると、曲 1、曲 2\ldots、曲 N の順に流れます。曲 N が流れ終わると、再び曲 1 から順に流れていきます。ある曲の途中で次の曲が流れることはなく、曲が流れ終わると、その瞬間に次の曲が流れ始めます。

プレイリストを再生してから T 秒後に流れているのはどの曲ですか?また、その曲が流れ始めてから何秒の時点ですか?
ただし、T 秒後ちょうどに曲が切り替わるような入力は与えられません。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • プレイリストを再生して T 秒後ちょうどに曲が切り替わることはない
  • 入力される値は全て整数

入力

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

N T
A_1 \ldots A_N

出力

プレイリストを再生してから T 秒後に流れている曲の番号と、その曲が流れ始めてから何秒たったかを表す整数を空白区切りで出力せよ。


入力例 1

3 600
180 240 120

出力例 1

1 60

プレイリストを再生してからの様子は次のようになります。

  • 0 秒後から 180 秒後まで曲 1 が流れる。
  • 180 秒後から 420 秒後まで曲 2 が流れる。
  • 420 秒後から 540 秒後まで曲 3 が流れる。
  • 540 秒後から 720 秒後まで曲 1 が流れる。
  • 720 秒後から 960 秒後まで曲 2 が流れる。
  • \qquad\vdots

600 秒後の時点で流れているのは曲 1 であり、流れ始めて 60 秒の時点です。


入力例 2

3 281
94 94 94

出力例 2

3 93

入力例 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

6 678912340

Score : 300 points

Problem Statement

We have a playlist with N songs numbered 1, \dots, N.
Song i lasts A_i seconds.

When the playlist is played, song 1, song 2, \ldots, and song N play in this order. When song N ends, the playlist repeats itself, starting from song 1 again. While a song is playing, the next song does not play; when a song ends, the next song starts immediately.

At exactly T seconds after the playlist starts playing, which song is playing? Also, how many seconds have passed since the start of that song?
There is no input where the playlist changes songs at exactly T seconds after it starts playing.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • The playlist does not change songs at exactly T seconds after it starts playing.
  • All values in the input are integers.

Input

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

N T
A_1 \ldots A_N

Output

Print an integer representing the song that is playing at exactly T seconds after the playlist starts playing, and an integer representing the number of seconds that have passed since the start of that song, separated by a space.


Sample Input 1

3 600
180 240 120

Sample Output 1

1 60

When the playlist is played, the following happens. (Assume that it starts playing at time 0.)

  • From time 0 to time 180, song 1 plays.
  • From time 180 to time 420, song 2 plays.
  • From time 420 to time 540, song 3 plays.
  • From time 540 to time 720, song 1 plays.
  • From time 720 to time 960, song 2 plays.
  • \qquad\vdots

At time 600, song 1 is playing, and 60 seconds have passed since the start of that song.


Sample Input 2

3 281
94 94 94

Sample Output 2

3 93

Sample Input 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

6 678912340
F - Previous Permutation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

(1, \dots, N) の順列 P = (P_1, \dots, P_N) が与えられます。ただし、(P_1, \dots, P_N) \neq (1, \dots, N) です。

(1 \dots, N) の順列を全て辞書順で小さい順に並べたとき、PK 番目であるとします。辞書順で小さい方から K-1 番目の順列を求めてください。

順列とは?

(1, \dots, N)順列とは、(1, \dots, N) を並べ替えて得られる数列のことをいいます。

辞書順とは?

長さ N の数列 A = (A_1, \dots, A_N), B = (B_1, \dots, B_N) に対し、AB より辞書順で真に小さいとは、ある整数 1 \leq i \leq N が存在して、下記の 2 つがともに成り立つことをいいます。

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
  • A_i < B_i

制約

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • 入力される値は全て整数

入力

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

N
P_1 \ldots P_N

出力

求める順列を Q = (Q_1, \dots, Q_N) として、Q_1, \dots, Q_N をこの順に空白区切りで一行に出力せよ。


入力例 1

3
3 1 2

出力例 1

2 3 1

(1, 2, 3) の順列を辞書順で小さい順に並べると次のようになります。

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

よって P = (3, 1, 2) は小さい方から 5 番目であり、求める順列、すなわち小さい方から 5 - 1 = 4 番目の順列は (2, 3, 1) です。


入力例 2

10
9 8 6 5 10 3 1 2 4 7

出力例 2

9 8 6 5 10 2 7 4 3 1

Score : 300 points

Problem Statement

You are given a permutation P = (P_1, \dots, P_N) of (1, \dots, N), where (P_1, \dots, P_N) \neq (1, \dots, N).

Assume that P is the K-th lexicographically smallest among all permutations of (1 \dots, N). Find the (K-1)-th lexicographically smallest permutation.

What are permutations?

A permutation of (1, \dots, N) is an arrangement of (1, \dots, N) into a sequence.

What is lexicographical order?

For sequences of length N, A = (A_1, \dots, A_N) and B = (B_1, \dots, B_N), A is said to be strictly lexicographically smaller than B if and only if there is an integer 1 \leq i \leq N that satisfies both of the following.

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1}).
  • A_i < B_i.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • All values in the input are integers.

Input

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

N
P_1 \ldots P_N

Output

Let Q = (Q_1, \dots, Q_N) be the sought permutation. Print Q_1, \dots, Q_N in a single line in this order, separated by spaces.


Sample Input 1

3
3 1 2

Sample Output 1

2 3 1

Here are the permutations of (1, 2, 3) in ascending lexicographical order.

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

Therefore, P = (3, 1, 2) is the fifth smallest, so the sought permutation, which is the fourth smallest (5 - 1 = 4), is (2, 3, 1).


Sample Input 2

10
9 8 6 5 10 3 1 2 4 7

Sample Output 2

9 8 6 5 10 2 7 4 3 1
G - Freefall

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

スーパーマンである高橋くんは、地上で困っている人を助けるため、あるビルの屋上から飛び降りようとしています。 高橋くんがいる星には重力の大きさを表す g という値が定まっており、 高橋くんが落下を開始してから地面に到達するまでにかかる時間は \frac{A}{\sqrt{g}} です。

現在の時刻は 0 であり、g = 1 が成り立ちます。 高橋くんは、今から次の操作を好きな回数(0 回でもよい)行います。

  • 超能力により g の値を 1 増やす。時間が B 経過する。

その後、高橋くんはビルから飛び降ります。落下を開始した後は g の値を変えることはできません。 また、操作によって経過する時間と落下にかかる時間以外は考えないものとします。

高橋くんが地面に到達できる最も早い時刻を求めてください。

制約

  • 1 \leq A \leq 10^{18}
  • 1 \leq B \leq 10^{18}
  • 入力は全て整数

入力

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

A B

出力

高橋くんが地面に到達できる最も早い時刻を出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

10 1

出力例 1

7.7735026919
  • 操作を 0 回行うとき、地面に到達する時刻は 1\times 0+\frac{10}{\sqrt{1}} = 10 です。
  • 操作を 1 回行うとき、地面に到達する時刻は 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07 です。
  • 操作を 2 回行うとき、地面に到達する時刻は 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77 です。
  • 操作を 3 回行うとき、地面に到達する時刻は 1\times 3+\frac{10}{\sqrt{4}} = 8 です。

操作を 4 回以上行っても、地面への到達時刻は遅くなるのみです。 よって、操作を 2 回行ってから飛び降りるのが最適で、答えは 2+\frac{10}{\sqrt{3}} です。


入力例 2

5 10

出力例 2

5.0000000000

操作を 1 回も行わないのが最適です。


入力例 3

1000000000000000000 100

出力例 3

8772053214538.5976562500

Score : 400 points

Problem Statement

A superman, Takahashi, is about to jump off the roof of a building to help a person in trouble on the ground. Takahashi's planet has a constant value g that represents the strength of gravity, and the time it takes for him to reach the ground after starting to fall is \frac{A}{\sqrt{g}}.

It is now time 0, and g = 1. Takahashi will perform the following operation as many times as he wants (possibly zero).

  • Use a superpower to increase the value of g by 1. This takes a time of B.

Then, he will jump off the building. After starting to fall, he cannot change the value of g. Additionally, we only consider the time it takes to perform the operation and fall.

Find the earliest time Takahashi can reach the ground.

Constraints

  • 1 \leq A \leq 10^{18}
  • 1 \leq B \leq 10^{18}
  • All values in the input are integers.

Input

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

A B

Output

Print the earliest time Takahashi can reach the ground. Your output will be accepted when its absolute or relative error from the true value is at most 10^{-6}.


Sample Input 1

10 1

Sample Output 1

7.7735026919
  • If he performs the operation zero times, he will reach the ground at time 1\times 0+\frac{10}{\sqrt{1}} = 10.
  • If he performs the operation once, he will reach the ground at time 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07.
  • If he performs the operation twice, he will reach the ground at time 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77.
  • If he performs the operation three times, he will reach the ground at time 1\times 3+\frac{10}{\sqrt{4}} = 8.

Performing the operation four or more times will only delay the time to reach the ground. Therefore, it is optimal to perform the operation twice before jumping off, and the answer is 2+\frac{10}{\sqrt{3}}.


Sample Input 2

5 10

Sample Output 2

5.0000000000

It is optimal not to perform the operation at all.


Sample Input 3

1000000000000000000 100

Sample Output 3

8772053214538.5976562500
H - Hit and Away

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

N 頂点 M 辺の単純連結無向グラフ G が与えられます。
G の頂点および辺はそれぞれ頂点 1,2,\ldots,N、辺 1,2,\ldots,M と番号づけられており、 辺 i は頂点 U_i と頂点 V_i を結んでいます。
辺で結ばれている頂点の間は双方向に時間 1 で移動することができます。

また、各頂点は安全か危険かのどちらかであり、その状態は SD のみからなる長さ N の文字列 S によって与えられます。
具体的には、Si 文字目 (1\leq i\leq N)S のとき頂点 i は安全であり、D のとき頂点 i は危険です。
ここで、安全な頂点が 2 つ以上、危険な頂点が 1 つ以上あることが保証されます。

危険な頂点 v それぞれについて、次の値を求めてください。

ある安全な頂点から出発し、v を経由し、 出発した頂点と異なる 安全な頂点へ移動するのにかかる時間としてあり得る最小値

制約

  • 3\leq N\leq 2\times 10^5
  • N-1\leq M\leq 2\times 10^5
  • 1\leq U_i,V_i\leq N
  • U_i\neq V_i
  • i\neq j ならば \{ U_i,V_i \}\neq \{ U_j,V_j \}
  • SSD のみからなる長さ N の文字列
  • N,M,U_i,V_i はすべて整数
  • G は連結である。
  • 安全な頂点が 2 つ以上存在する。
  • 危険な頂点が 1 つ以上存在する。

入力

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

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M
S

出力

G 上の危険な頂点の個数を K として、K 行出力せよ。
i 行目 (1\leq i\leq K) には、危険な頂点を頂点番号の昇順に並べた時に i 番目に来る頂点についての答えを出力せよ。


入力例 1

5 5
1 2
1 3
2 3
3 4
4 5
SSDDS

出力例 1

2
3

危険な頂点は(頂点番号について昇順に)頂点 34 です。

頂点 3 については、頂点 1 \to 頂点 3 \to 頂点 2 と移動するときなどが条件をみたします。
この移動にかかる時間は 2 であり、このときが最小です。
よって、1 行目に 2 を出力します。

頂点 4 については、頂点 1 \to 頂点 3 \to 頂点 4 \to 頂点 5 と移動するときなどが条件をみたします。
この移動にかかる時間は 3 であり、かかる時間が 2 以下の条件をみたす移動方法がないため、このときが最小です。
よって、2 行目に 3 を出力します。


入力例 2

3 2
1 2
2 3
SSD

出力例 2

3

危険な頂点は頂点 3 です。

頂点 1 \to 頂点 2 \to 頂点 3 \to 頂点 2 と移動するときなどが条件をみたします。
この移動にかかる時間は 3 であり、このときが最小です。
頂点 2 \to 頂点 3 \to 頂点 2 などの移動は、「出発した頂点と異なる」という条件をみたしていないことに注意してください。

Score : 450 points

Problem Statement

You are given a simple connected undirected graph G with N vertices and M edges.
The vertices and edges of G are numbered as vertices 1,2,\ldots,N and edges 1,2,\ldots,M, respectively, and edge i connects vertices U_i and V_i.
You can move bidirectionally between vertices connected by an edge in time 1.

Additionally, each vertex is either safe or dangerous, and this state is given by a string S of length N consisting of S and D.
Specifically, vertex i is safe when the i-th character (1\leq i\leq N) of S is S, and vertex i is dangerous when it is D.
It is guaranteed that there are at least two safe vertices and at least one dangerous vertex.

For each dangerous vertex v, find the following value:

The minimum possible time to start from some safe vertex, pass through v, and move to a safe vertex different from the starting vertex.

Constraints

  • 3\leq N\leq 2\times 10^5
  • N-1\leq M\leq 2\times 10^5
  • 1\leq U_i,V_i\leq N
  • U_i\neq V_i
  • If i\neq j, then \{ U_i,V_i \}\neq \{ U_j,V_j \}.
  • S is a string of length N consisting of S and D.
  • N,M,U_i,V_i are all integers.
  • G is connected.
  • There are at least two safe vertices.
  • There is at least one dangerous vertex.

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
S

Output

Let K be the number of dangerous vertices in G, and print K lines.
On the i-th line (1\leq i\leq K), print the answer for the i-th dangerous vertex when the dangerous vertices are arranged in ascending order of vertex number.


Sample Input 1

5 5
1 2
1 3
2 3
3 4
4 5
SSDDS

Sample Output 1

2
3

The dangerous vertices are (in ascending order of vertex number) vertices 3 and 4.

For vertex 3, moving from vertex 1 \to vertex 3 \to vertex 2 (for example) satisfies the condition.
The time required for this movement is 2, and this is the minimum.
Therefore, print 2 on the 1st line.

For vertex 4, moving from vertex 1 \to vertex 3 \to vertex 4 \to vertex 5 (for example) satisfies the condition.
The time required for this movement is 3, and there is no way to move that satisfies the condition with time 2 or less, so this is the minimum.
Therefore, print 3 on the 2nd line.


Sample Input 2

3 2
1 2
2 3
SSD

Sample Output 2

3

The dangerous vertex is vertex 3.

Moving from vertex 1 \to vertex 2 \to vertex 3 \to vertex 2 (for example) satisfies the condition.
The time required for this movement is 3, and this is the minimum.
Note that movements such as vertex 2 \to vertex 3 \to vertex 2 do not satisfy the condition that the destination is "different from the starting vertex".

I - Back and Forth Filling

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

整数 N と長さ N-1L, R からなる文字列 S が与えられます。

横一列に並んだ N 個のマス目に、以下の条件を全て満たすように整数を書き込むことを考えます。

  • 全てのマスに整数が 1 つ書かれている。
  • 整数 1,2,\dots,N が書かれているマスが 1 つずつ存在する。
  • Si 文字目 が L であるとき、 i+1i より左に書き込まれている。
  • Si 文字目 が R であるとき、 i+1i より右に書き込まれている。

C_i を「左から i マス目に書き込まれる整数としてありうるものの個数」とします。 C_1,C_2,\dots,C_N を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 20000
  • 2 \le N \le 3 \times 10^5
  • S は長さ N-1L, R からなる文字列
  • ひとつの入力について、 N の総和は 3 \times 10^5 を超えない

入力

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

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

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

N
S

出力

T 行出力せよ。

i 行目には i 番目のテストケースについて、答えを以下の形式で出力せよ。

C_1 C_2 \dots C_N

入力例 1

5
5
RLLR
3
RL
2
L
3
RR
20
RLLLLLLLLRLRRLLLRLR

出力例 1

2 4 3 4 2
2 2 1
1 1
1 1 1
5 9 13 14 15 17 18 19 19 20 20 19 19 18 17 16 14 12 9 5

この入力には 5 個のテストケースが含まれます。

  • 1 番目のテストケースにおいて、マス目の埋め方として考えられるものは以下の 11 個です。
    • (1,4,3,2,5)
    • (1,4,3,5,2)
    • (1,4,5,3,2)
    • (4,1,3,2,5)
    • (4,1,3,5,2)
    • (4,1,5,3,2)
    • (4,3,1,2,5)
    • (4,3,1,5,2)
    • (4,3,5,1,2)
    • (4,5,1,3,2)
    • (4,5,3,1,2)
  • ここから、 C_i の各値は次の通りに求まります。
    • 左から 1 マス目に書き込まれる整数としてありうるものは 1,42 個です。
    • 左から 2 マス目に書き込まれる整数としてありうるものは 1,3,4,54 個です。
    • 左から 3 マス目に書き込まれる整数としてありうるものは 1,3,53 個です。
    • 左から 4 マス目に書き込まれる整数としてありうるものは 1,2,3,54 個です。
    • 左から 5 マス目に書き込まれる整数としてありうるものは 2,52 個です。
  • 2 番目のテストケースにおいて、マス目の埋め方として考えられるものは以下の 2 通りです。
    • (1,3,2)
    • (3,1,2)
  • 3 番目のテストケースにおいて、マス目の埋め方として考えられるものは以下の 1 通りです。
    • (2,1)
  • 4 番目のテストケースにおいて、マス目の埋め方として考えられるものは以下の 1 通りです。
    • (1,2,3)

Score : 500 points

Problem Statement

You are given an integer N and a string S of length N-1 consisting of L and R.

Consider writing integers into N cells arranged in a row so that the following conditions are satisfied:

  • Every cell has one integer written on it.
  • Every integer 1,2,\dots,N appears in exactly one cell.
  • When the i-th character of S is L, i+1 is written to the left of i.
  • When the i-th character of S is R, i+1 is written to the right of i.

Let C_i be the number of integers that can be written in the i-th cell from the left. Find C_1,C_2,\dots,C_N.

You are given T test cases; find the answer for each of them.

Constraints

  • 1 \le T \le 20000
  • 2 \le N \le 3 \times 10^5
  • S is a string of length N-1 consisting of L and R.
  • For a single input, the sum of N does not exceed 3 \times 10^5.

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
S

Output

Print T lines.

The i-th line should contain the answer for the i-th test case in the following format:

C_1 C_2 \dots C_N

Sample Input 1

5
5
RLLR
3
RL
2
L
3
RR
20
RLLLLLLLLRLRRLLLRLR

Sample Output 1

2 4 3 4 2
2 2 1
1 1
1 1 1
5 9 13 14 15 17 18 19 19 20 20 19 19 18 17 16 14 12 9 5

This input contains five test cases.

  • For the first test case, there are 11 possible ways to fill the cells as follows:
    • (1,4,3,2,5)
    • (1,4,3,5,2)
    • (1,4,5,3,2)
    • (4,1,3,2,5)
    • (4,1,3,5,2)
    • (4,1,5,3,2)
    • (4,3,1,2,5)
    • (4,3,1,5,2)
    • (4,3,5,1,2)
    • (4,5,1,3,2)
    • (4,5,3,1,2)
  • From this, each value of C_i is determined as follows:
    • The integers that can be written in the 1-st cell from the left are 1,4, which is two integers.
    • The integers that can be written in the 2-nd cell from the left are 1,3,4,5, which is four integers.
    • The integers that can be written in the 3-rd cell from the left are 1,3,5, which is three integers.
    • The integers that can be written in the 4-th cell from the left are 1,2,3,5, which is four integers.
    • The integers that can be written in the 5-th cell from the left are 2,5, which is two integers.
  • For the second test case, there are two possible ways to fill the cells as follows:
    • (1,3,2)
    • (3,1,2)
  • For the third test case, there is one possible way to fill the cells as follows:
    • (2,1)
  • For the fourth test case, there is one possible way to fill the cells as follows:
    • (1,2,3)