A - Piling Up

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

AtCoder では、ユーザーのレートは正の整数で与えられ、その値に応じて ^ がいくつか表示されます。 特に、レートが 1 以上 399 以下のときは次のようになります。

  • レートが 1 以上 99 以下のときは ^ は 1 個表示される。
  • レートが 100 以上 199 以下のときは ^ は 2 個表示される。
  • レートが 200 以上 299 以下のときは ^ は 3 個表示される。
  • レートが 300 以上 399 以下のときは ^ は 4 個表示される。

現在の高橋君のレートは R です。ここで、R は 1 以上 299 以下の整数であることが保証されます。
表示される ^ の数を現在より増やすために、高橋君は少なくともレートをいくつ上げる必要があるか答えてください。
なお、本問題の制約下で、高橋君はレートを 400 以上に上げることなく ^ の数を増やすことができることが証明できます。

制約

  • 1\leq R \leq 299
  • R は整数

入力

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

R

出力

表示される ^ の数を現在より増やすために、高橋君は少なくともレートをいくつ上げる必要があるか整数で出力せよ。


入力例 1

123

出力例 1

77

高橋君の現在のレートは 123 であり、^ は 2 個表示されています。
ここからレートを 77 上げると高橋君のレートは 200 となり、^ は 3 個表示されるようになります。 レート が 199 以下のときは ^ の数は 2 個以下であるので、77 を出力します。


入力例 2

250

出力例 2

50

Score : 100 points

Problem Statement

In AtCoder, a user's rating is given as a positive integer, and based on this value, a certain number of ^ is displayed. Specifically, when the rating is between 1 and 399, inclusive, the display rules are as follows:

  • When the rating is between 1 and 99, inclusive, ^ is displayed once.
  • When the rating is between 100 and 199, inclusive, ^ is displayed twice.
  • When the rating is between 200 and 299, inclusive, ^ is displayed three times.
  • When the rating is between 300 and 399, inclusive, ^ is displayed four times.

Currently, Takahashi's rating is R. Here, it is guaranteed that R is an integer between 1 and 299, inclusive.
Find the minimum increase in rating required for him to increase the number of displayed ^.
It can be proved that under the constraints of this problem, he can increase the number of ^ without raising his rating to 400 or above.

Constraints

  • 1 \leq R \leq 299
  • R is an integer.

Input

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

R

Output

Print, as an integer, the minimum increase in rating required for Takahashi to increase the number of displayed ^.


Sample Input 1

123

Sample Output 1

77

Takahashi's current rating is 123, and ^ is displayed twice.
By increasing his rating by 77, his rating will become 200, and ^ will be displayed three times. When the rating is 199 or below, ^ is displayed not more than twice, so print 77.


Sample Input 2

250

Sample Output 2

50
B - camel Case

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英大文字および英小文字からなる文字列 S が与えられます。
ここで、 S のうちちょうど 1 文字だけが英大文字であり、それ以外は全て英小文字です。
大文字が S の先頭から何文字目に登場するか出力してください。
ただし、S の先頭の文字を 1 文字目とします。

制約

  • S は英大文字および英小文字からなる長さ 2 以上 100 以下の文字列
  • S に大文字はちょうど 1 つ含まれる。

入力

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

S

出力

大文字が S の先頭から何文字目に登場するかを、整数で出力せよ。


入力例 1

aBc

出力例 1

2

aBc の先頭から 1 文字目は a , 2 文字目は B , 3 文字目は c であり、 このうち大文字は 2 文字目です。
よって、2 を出力します。


入力例 2

xxxxxxXxxx

出力例 2

7

S=xxxxxxXxxx の 7 文字目に、大文字である X が登場しています。よって、7 を出力します。


入力例 3

Zz

出力例 3

1

Score : 100 points

Problem Statement

You are given a string S consisting of uppercase and lowercase English letters.
Here, exactly one character of S is uppercase, and the others are all lowercase.
Find the integer x such that the x-th character of S is uppercase.
Here, the initial character of S is considered the 1-st one.

Constraints

  • S is a string of length between 2 and 100, inclusive, consisting of uppercase and lowercase English letters.
  • S has exactly one uppercase letter.

Input

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

S

Output

Print the integer x such that the x-th character of S is uppercase.


Sample Input 1

aBc

Sample Output 1

2

The 1-st character of aBc is a, the 2-nd is B, and the 3-rd is c; the 2-nd character is uppercase.
Thus, 2 should be printed.


Sample Input 2

xxxxxxXxxx

Sample Output 2

7

An uppercase letter X occurs as the 7-th character of S=xxxxxxXxxx, so 7 should be printed.


Sample Input 3

Zz

Sample Output 3

1
C - Number Box

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

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

N 行 N 列のマス目があり、上から i 行目、左から j 列目のマスには数字 A_{i,j} が書かれています。

このマス目は上下および左右がつながっているものとします。つまり以下が全て成り立ちます。

  • (1,i) の上のマスは (N,i) であり、(N,i) の下のマスは (1,i) である。(1\le i\le N)
  • (i,1) の左のマスは (i,N) であり、(i,N) の右のマスは (i,1) である。(1\le i\le N)

高橋君は、上下左右および斜めの 8 方向のうちいずれかを初めに選びます。そして、好きなマスから決めた方向に 1 マス移動することを N-1 回繰り返します。

高橋君は N 個のマス上を移動することになりますが、高橋君が通ったマスに書かれている数字を左から通った順番に並べた整数としてあり得る最大のものを求めてください。

制約

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • 入力はすべて整数。

入力

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

出力

答えを出力せよ。


入力例 1

4
1161
1119
7111
1811

出力例 1

9786

高橋君が上から 2 行目、左から 4 列目のマスから出発し、右下に進むことで、通ったマスに書かれた数字を並べ 9786 を作ることができます。 9786 より大きい値を作ることはできないため、9786 が解です。


入力例 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

出力例 2

1111111111

32bit整数型に答えが収まるとは限らないことに注意してください。

Score : 200 points

Problem Statement

You are given a positive integer N.

We have a grid with N rows and N columns, where the square at the i-th row from the top and j-th column from the left has a digit A_{i,j} written on it.

Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds.

  • (N,i) is just above (1,i), and (1,i) is just below (N,i). (1\le i\le N).
  • (i,N) is just to the left of (i,1), and (i,1) is just to the right of (i,N). (1\le i\le N).

Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1 times.

In this process, Takahashi visits N squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.

Constraints

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

Output

Print the answer.


Sample Input 1

4
1161
1119
7111
1811

Sample Output 1

9786

If Takahashi starts on the square at the 2-nd row from the top and 4-th column from the left and goes down and to the right, the integer obtained by arranging the digits written on the visited squares will be 9786. It is impossible to make a value greater than 9786, so the answer is 9786.


Sample Input 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

Sample Output 2

1111111111

Note that the answer may not fit into a 32-bit integer.

D - Minimize Ordering

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

文字列 S が与えられます。S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力してください。

なお、相異なる 2 つの文字列 s = s_1 s_2 \ldots s_n と t = t_1 t_2 \ldots t_m について、それらが以下の条件のいずれかを満たすとき、辞書順で s \lt t であるとします。

  • ある整数 i\ (1 \leq i \leq \min(n,m)) が存在し、s_i \lt t_i かつすべての整数 j\ (1 \leq j \lt i) について s_j=t_j
  • すべての整数 i\ (1 \leq i \leq \min(n,m)) について s_i = t_i かつ、n \lt m

制約

  • S は英小文字のみからなる長さ 1 以上 2 \times 10^5 以下の文字列

入力

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

S

出力

S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力せよ。


入力例 1

aba

出力例 1

aab

S= aba を並び替えて得られる文字列は以下の 3 つが考えられます。

  • aba
  • aab
  • baa

この中で辞書順で最小のものは、aab です。


入力例 2

zzzz

出力例 2

zzzz

Score : 200 points

Problem Statement

You are given a string S. Find the lexicographically smallest string S' obtained by permuting the characters of S.

Here, for different two strings s = s_1 s_2 \ldots s_n and t = t_1 t_2 \ldots t_m, s \lt t holds lexicographically when one of the conditions below is satisfied.

  • There is an integer i\ (1 \leq i \leq \min(n,m)) such that s_i \lt t_i and s_j=t_j for all integers j\ (1 \leq j \lt i).
  • s_i = t_i for all integers i\ (1 \leq i \leq \min(n,m)), and n \lt m.

Constraints

  • S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest string S' obtained by permuting the characters in S.


Sample Input 1

aba

Sample Output 1

aab

Three strings can be obtained by permuting aba:

  • aba
  • aab
  • baa

The lexicographically smallest among them is aab.


Sample Input 2

zzzz

Sample Output 2

zzzz
E - Dislike Foods

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

AtCoderレストランでは 1 から N までの番号がつけられている N 種類の食材を扱っています。

また、AtCoderレストランでは 1 から M までの番号がつけられている M 個の料理を提供しています。料理 i には K_i 種類の食材が使われており、食材 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} が使われています。

すぬけくんは現在 N 種類の食材がすべて苦手です。また、すぬけ君は苦手な食材が 1 種類でも使われている料理を食べることができず、苦手な食材が 1 種類も使われていない料理を食べることができます。

すぬけ君はこれから N 日間かけて苦手な食材を克服しようとしています。 すぬけ君は i 日目に食材 B_i を克服し、それ以降苦手な食材でなくなります。

i=1,2,\ldots,N について以下の値を求めてください。

  • i 日目にすぬけ君が食材 B_i を克服した直後、すぬけ君が食べることができるAtCoderレストランの料理の個数

制約

  • 1 \leq N \leq 3 \times 10^{5}
  • 1 \leq M \leq 3 \times 10^{5}
  • 1 \leq K_i \leq N (1 \leq i \leq M)
  • K_i の総和は 3 \times 10^{5} 以下
  • 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
  • A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
  • 1 \leq B_i \leq N (1 \leq i \leq N)
  • B_i \neq B_j (i \neq j )
  • 入力は全て整数

入力

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

N M
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N

出力

N 行出力せよ。k 行目には、i=k のときの値を出力せよ。


入力例 1

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

出力例 1

0
1
2
3
4

以下のようにすぬけ君は苦手な食材を克服します。

  • 1 日目: 食材 1 を克服する。このとき、どの料理にも苦手な食材が使われているため 0 を出力する。
  • 2 日目: 食材 3 を克服する。このとき、料理 4 は苦手な食材が使われなくなるため食べられるようになる。料理 4 以外の料理は苦手な食材が使われているため、 1 を出力する。
  • 3 日目: 食材 2 を克服する。このとき、料理 1 は苦手な食材が使われなくなるため食べられるようになる。料理 1,4 以外の料理は苦手な食材が使われているため、 2 を出力する。
  • 4 日目: 食材 5 を克服する。このとき、料理 3 は苦手な食材が使われなくなるため食べられるようになる。料理 1,3,4 以外の料理は苦手な食材が使われているため、 3 を出力する。
  • 5 日目: 食材 4 を克服する。このとき、料理 2 は苦手な食材が使われなくなるため食べられるようになる。全ての料理に苦手な食材が使われていないため、 4 を出力する。

入力例 2

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

出力例 2

0
0
1
1
1
2
4
6
8

Score : 300 points

Problem Statement

The AtCoder Restaurant uses N types of ingredients numbered from 1 to N.

The restaurant offers M dishes numbered from 1 to M. Dish i uses K_i types of ingredients, namely A_{i,1}, A_{i,2}, \ldots, A_{i,K_i}.

Snuke currently dislikes all N ingredients. He cannot eat any dish that uses one or more ingredients he dislikes, and he can eat a dish that uses none of the disliked ingredients.

Over the next N days, he will overcome his dislikes one ingredient per day. On day i, he overcomes ingredient B_i, and from then on he no longer dislikes it.

For each i=1,2,\ldots,N, find:

  • the number of dishes at the AtCoder Restaurant that he can eat immediately after overcoming ingredient B_i on day i.

Constraints

  • 1 \leq N \leq 3 \times 10^{5}
  • 1 \leq M \leq 3 \times 10^{5}
  • 1 \leq K_i \leq N (1 \leq i \leq M)
  • The sum of K_i is at most 3 \times 10^{5}.
  • 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
  • A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
  • 1 \leq B_i \leq N (1 \leq i \leq N)
  • B_i \neq B_j (i \neq j )
  • All input values are integers.

Input

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

N M
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N

Output

Print N lines. The k-th line should contain the answer for i=k.


Sample Input 1

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

Sample Output 1

0
1
2
3
4

Snuke overcomes his disliked ingredients as follows:

  • Day 1: He overcomes ingredient 1. At this time, every dish still uses a disliked ingredient, so print 0.
  • Day 2: He overcomes ingredient 3. Dish 4 no longer uses any disliked ingredient and becomes edible; all other dishes still use disliked ingredients, so print 1.
  • Day 3: He overcomes ingredient 2. Dish 1 no longer uses any disliked ingredient and becomes edible; all dishes except 1 and 4 still use disliked ingredients, so print 2.
  • Day 4: He overcomes ingredient 5. Dish 3 no longer uses any disliked ingredient and becomes edible; all dishes except 1, 3, and 4 still use disliked ingredients, so print 3.
  • Day 5: He overcomes ingredient 4. Dish 2 no longer uses any disliked ingredient and becomes edible; now all dishes have no disliked ingredients, so print 4.

Sample Input 2

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

Sample Output 2

0
0
1
1
1
2
4
6
8
F - False Hope

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

3\times3 のマス目に 1 から 9 までの数字が書き込まれており、上から i 行目、左から j 列目 (1\leq i\leq3,1\leq j\leq3) に書き込まれている数字は c _ {i,j} です。

異なるマスに同じ数字が書き込まれている場合もありますが、同じ数字が縦・横・斜めに 3 つ連続して書き込まれていることはありません。 より厳密には、c _ {i,j} について次の条件のすべてが成り立っていることが保証されます。

  • どの 1\leq i\leq3 についても、c _ {i,1}=c _ {i,2}=c _ {i,3} ではない
  • どの 1\leq j\leq3 についても、c _ {1,j}=c _ {2,j}=c _ {3,j} ではない
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {3,1}=c _ {2,2}=c _ {1,3} ではない

高橋くんは、それぞれのマスに書かれている数字をランダムな順番で知ります。 高橋くんは、縦・横・斜めの列のうちの 1 つでも次の条件を満たしたときがっかりします。

  • はじめに知ったほうの 2 マスに書かれた数字が同じであり、最後に知ったマスに書かれた数字がそれと異なる。

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を求めてください。

制約

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} ではない (1\leq i\leq3)
  • c _ {1,j}=c _ {2,j}=c _ {3,j} ではない (1\leq j\leq3)
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {1,3}=c _ {2,2}=c _ {3,1} ではない

入力

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

出力

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を 1 行で出力せよ。 真の値からの絶対誤差が 10 ^ {-8} 以下であるとき、正答と判定される。


入力例 1

3 1 9
2 5 6
2 7 1

出力例 1

0.666666666666666666666666666667

例えば、高橋くんが c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 の順に知った場合、高橋くんはがっかりしてしまいます。

対して、高橋くんが c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} の順に数字を知った場合、がっかりすることなくすべての数字を知ることができます。

高橋くんががっかりすることなくすべての数字を知ることができる確率は \dfrac 23 です。 絶対誤差が 10 ^ {-8} 以下であれば正答と判定されるため、0.666666657 や 0.666666676 のように出力しても正解になります。


入力例 2

7 7 6
8 6 8
7 7 6

出力例 2

0.004982363315696649029982363316

入力例 3

3 6 7
1 9 7
5 7 5

出力例 3

0.4

Score : 300 points

Problem Statement

There is a 3\times3 grid with numbers between 1 and 9, inclusive, written in each square. The square at the i-th row from the top and j-th column from the left (1\leq i\leq3,1\leq j\leq3) contains the number c _ {i,j}.

The same number may be written in different squares, but not in three consecutive cells vertically, horizontally, or diagonally. More precisely, it is guaranteed that c _ {i,j} satisfies all of the following conditions.

  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Takahashi will see the numbers written in each cell in random order. He will get disappointed when there is a line (vertical, horizontal, or diagonal) that satisfies the following condition.

  • The first two squares he sees contain the same number, but the last square contains a different number.

Find the probability that Takahashi sees the numbers in all the squares without getting disappointed.

Constraints

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Input

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

Output

Print one line containing the probability that Takahashi sees the numbers in all the squares without getting disappointed. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}.


Sample Input 1

3 1 9
2 5 6
2 7 1

Sample Output 1

0.666666666666666666666666666667

For example, if Takahashi sees c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 in this order, he will get disappointed.

On the other hand, if Takahashi sees c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} in this order, he will see all numbers without getting disappointed.

The probability that Takahashi sees all the numbers without getting disappointed is \dfrac 23. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}, so outputs such as 0.666666657 and 0.666666676 would also be accepted.


Sample Input 2

7 7 6
8 6 8
7 7 6

Sample Output 2

0.004982363315696649029982363316

Sample Input 3

3 6 7
1 9 7
5 7 5

Sample Output 3

0.4
G - Tying Rope

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

一方の端が赤に塗られており、もう一方の端が青に塗られているロープが N 本あります。ロープには 1 から N までの番号がつけられています。

これからロープの端を結ぶ操作を M 回行います。i 回目の操作ではロープ A_i の色 B_i の端とロープ C_i の色 D_i の端を結びます。ただし、色 R は赤を意味し、色 B は青を意味します。各ロープについて、同じ色の端が複数回結ばれることはありません。

すべての操作を終えた後に、ひとつながりになっているロープの組について環状になっているものとそうでないものの個数を出力してください。

ただし、ひとつながりになっているロープの組 \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace が環状になっているとは、v の要素の順序を適切に入れ替えることで各 0 \leq i < x についてロープ v_i とロープ v_{(i+1) \bmod x} が結ばれているようにできることをいいます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq N
  • (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
  • (A_i, B_i) \neq (C_j, D_j)
  • N, M, A_i, C_i は整数
  • B_i, D_i は R か B のいずれか

入力

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

N M
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_M B_M C_M D_M

出力

ひとつながりになっているロープの組について、環状になっているものの個数を X、そうでないものの個数を Y として X と Y をこの順に空白区切りで出力せよ。


入力例 1

5 3
3 R 5 B
5 R 3 B
4 R 2 B

出力例 1

1 2

ひとつながりになっているロープの組は \lbrace 1 \rbrace、\lbrace 2,4 \rbrace、\lbrace 3,5 \rbrace の 3 つです。

ロープ \lbrace 3,5 \rbrace の組は環状になっており、ロープ \lbrace 1 \rbrace と \lbrace 2,4 \rbrace の組は環状になっていません。したがって、X = 1, Y = 2 です。


入力例 2

7 0

出力例 2

0 7

入力例 3

7 6
5 R 3 R
7 R 4 R
4 B 1 R
2 R 3 B
2 B 5 B
1 B 7 B

出力例 3

2 1

Score : 400 points

Problem Statement

There are N ropes numbered 1 through N. One end of each rope is painted red, and the other is painted blue.

You are going to perform M operations of tying ropes. In the i-th operation, you tie the end of rope A_i painted B_i with the end of rope C_i painted D_i, where R means red and B means blue. For each rope, an end with the same color is not tied multiple times.

Find the number of groups of connected ropes that form cycles, and the number of those that do not, after all the operations.

Here, a group of connected ropes \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace is said to form a cycle if one can rearrange the elements of v so that, for each 0 \leq i < x, rope v_i is tied to rope v_{(i+1) \bmod x}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq N
  • (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
  • (A_i, B_i) \neq (C_j, D_j)
  • N, M, A_i, and C_i are integers.
  • B_i is R or B, and so is D_i.

Input

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

N M
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_M B_M C_M D_M

Output

Print X and Y in this order, separated by a space, where X is the number of groups of connected ropes that form cycles, and Y is the number of those that do not.


Sample Input 1

5 3
3 R 5 B
5 R 3 B
4 R 2 B

Sample Output 1

1 2

There are three groups of connected ropes: \lbrace 1 \rbrace, \lbrace 2,4 \rbrace, and \lbrace 3,5 \rbrace.

The group of ropes \lbrace 3,5 \rbrace forms a cycle, while the groups of rope \lbrace 1 \rbrace and ropes \lbrace 2,4 \rbrace do not. Thus, X = 1 and Y = 2.


Sample Input 2

7 0

Sample Output 2

0 7

Sample Input 3

7 6
5 R 3 R
7 R 4 R
4 B 1 R
2 R 3 B
2 B 5 B
1 B 7 B

Sample Output 3

2 1
H - Cables and Servers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

1 から N の番号がついた N 台のサーバーと 1 から M の番号がついた M 本のケーブルがあります。
ケーブル i はサーバー A_i とサーバー B_i を双方向に結んでいます。

以下の操作を何回か( 0 回でもよい)行うことで、全てのサーバー同士がケーブルを介して繋がっているようにしてください。

  • 操作:ケーブルを 1 つ選び、その片方の端を別のサーバーに繋ぎ変える

操作の最小回数と、最小回数となる操作列を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • 入力は全て整数である

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

操作回数の最小値を K とする。K+1 行出力せよ。
1 行目には K を出力せよ。
i+1 行目には i 回目の操作で選ぶケーブルの番号、繋ぎ変える前に繋がっていたサーバーの番号、繋ぎ変えたあと繋がっているサーバーの番号をこの順に空白区切りで出力せよ。

条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

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

出力例 1

1
1 1 3

ケーブル 1 のサーバー 1 に繋がっている側の端をサーバー 3 に繋ぎ変えることで、サーバー同士がケーブルを介して繋がっている状態にすることができます。

図

このほか、「ケーブル 5 のサーバー 4 に繋がっている側の端をサーバー 1 に繋ぎ変える」という操作や、「ケーブル 2 のサーバー 2 に繋がっている側の端をサーバー 3 に繋ぎ変える」という操作でもサーバー同士がケーブルを介して繋がっている状態にすることができるため、正解とみなされます。


入力例 2

4 3
3 4
4 1
1 2

出力例 2

0

操作をする必要がないこともあります。


入力例 3

5 4
3 3
3 3
3 3
3 3

出力例 3

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

Score : 450 points

Problem Statement

There are N servers numbered from 1 to N and M cables numbered from 1 to M.
Cable i connects servers A_i and B_i bidirectionally.

By performing the following operation some number of times (possibly zero), make all servers connected via cables.

  • Operation: Choose one cable and reconnect one of its ends to a different server.

Find the minimum number of operations required and output an operation sequence achieving this minimum.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq N
  • All input values are integers.

Input

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Let the minimum number of operations be K. Print K+1 lines.

  • The first line should contain K.
  • The (i+1)-th line should contain three space-separated integers: the number of the cable chosen in the i-th operation, the server number that was originally connected at that end, and the server number to which it is connected after the operation, in this order.

If there are multiple valid solutions, any one of them will be accepted.


Sample Input 1

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

Sample Output 1

1
1 1 3

By reconnecting the end of cable 1 that is connected to server 1 to server 3, the servers can be connected via cables.

図

Operations such as reconnecting the end of cable 5 that is connected to server 4 to server 1, or reconnecting the end of cable 2 that is connected to server 2 to server 3, will also result in all servers being connected and are considered correct.


Sample Input 2

4 3
3 4
4 1
1 2

Sample Output 2

0

No operation may be necessary.


Sample Input 3

5 4
3 3
3 3
3 3
3 3

Sample Output 3

4
1 3 5
2 3 4
3 3 2
4 3 1
I - Concat (2nd)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 575 点

問題文

N 個の英小文字からなる文字列 S_1,S_2,\dots,S_N が与えられます。

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) としてありえるもの全てについて、以下の通りに生成される文字列を書き並べます。

  • S_{P_1},S_{P_2},\dots,S_{P_N} をこの順に連結する。

書き並べられた N! 個の文字列を辞書順に並べた列を A_1,A_2,\dots,A_{N!} とします。
A_2 を出力してください。

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

制約

  • 1 \le T \le 1.5 \times 10^5
  • 2 \le N \le 3 \times 10^5
  • T,N は整数
  • S_i は英小文字からなる長さ 1 以上 10^6-1 以下の文字列
  • ひとつの入力について、 N の総和は 3 \times 10^5 を超えない
  • ひとつの入力について、全てのテストケースにおける |S_i| の総和は 10^6 を超えない

入力

入力は以下の形式で標準入力から与えられる。
\text{case}_i は i 番目のテストケースを表す。

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

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

N
S_1
S_2
\vdots
S_N

出力

T 行出力せよ。

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


入力例 1

3
3
abc
ac
ahc
4
aaa
a
aaaa
a
15
ks
sy
k
ysk
yks
ky
ksy
sk
syk
s
kys
sky
ys
yk
y

出力例 1

abcahcac
aaaaaaaaa
kksksykykysskskyssyksyyksykyskyys

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

1 番目のテストケースについて、 S=( abc, ac, ahc ) です。
A=( abcacahc, abcahcac, acabcahc, acahcabc, ahcabcac, ahcacabc ) なので、 A_2= abcahcac を出力します。

Score : 575 points

Problem Statement

You are given N strings S_i consisting of lowercase English letters.

For all possible permutations P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N), write down the string generated as follows:

  • Concatenate S_{P_1},S_{P_2},\dots,S_{P_N} in this order.

Let A_1,A_2,\dots,A_{N!} be the sequence of the N! written strings sorted in lexicographical order.
Output A_2.

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

Constraints

  • 1 \le T \le 1.5 \times 10^5
  • 2 \le N \le 3 \times 10^5
  • T,N are integers.
  • S_i is a string consisting of lowercase English letters with length between 1 and 10^6-1, inclusive.
  • For a single input, the sum of N does not exceed 3 \times 10^5.
  • For a single input, the sum of |S_i| does not exceed 10^6.

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_1
S_2
\vdots
S_N

Output

Output T lines.

The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
3
abc
ac
ahc
4
aaa
a
aaaa
a
15
ks
sy
k
ysk
yks
ky
ksy
sk
syk
s
kys
sky
ys
yk
y

Sample Output 1

abcahcac
aaaaaaaaa
kksksykykysskskyssyksyyksykyskyys

This input contains three test cases.

For the first test case, S=( abc, ac, ahc ).
We have A=( abcacahc, abcahcac, acabcahc, acahcabc, ahcabcac, ahcacabc ), so output A_2= abcahcac.