実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
以下の条件を満たす正整数 x を 321-like Number と呼びます。
- x の各桁を上から見ると狭義単調減少になっている。
- すなわち、x が d 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
- ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )
なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。
例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。
N が入力として与えられるので、 N が 321-like Number なら Yes 、そうでないなら No と出力してください。
制約
- 入力は全て整数
- 1 \le N \le 99999
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N が 321-like Number なら Yes 、そうでないなら No と出力せよ。
入力例 1
321
出力例 1
Yes
N=321 に対して、以下が成り立ちます。
- 上から 1 桁目の 3 は上から 2 桁目の 2 より大きい。
- 上から 2 桁目の 2 は上から 3 桁目の 1 より大きい。
以上より、 321 は 321-like Number です。
入力例 2
123
出力例 2
No
N=123 について、例えば以下が成り立ちます。
- 上から 1 桁目の 1 は上から 2 桁目の 2 より大きくない。
このことから、 123 は 321-like Number ではありません。
入力例 3
1
出力例 3
Yes
入力例 4
86411
出力例 4
No
Score : 100 points
Problem Statement
A positive integer x is called a 321-like Number when it satisfies the following condition.
- The digits of x are strictly decreasing from top to bottom.
- In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
- (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).
Note that all one-digit positive integers are 321-like Numbers.
For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.
You are given N as input. Print Yes if N is a 321-like Number, and No otherwise.
Constraints
- All input values are integers.
- 1 \le N \le 99999
Input
The input is given from Standard Input in the following format:
N
Output
Print Yes if N is a 321-like Number, and No otherwise.
Sample Input 1
321
Sample Output 1
Yes
For N=321, the following holds:
- The first digit from the top, 3, is greater than the second digit from the top, 2.
- The second digit from the top, 2, is greater than the third digit from the top, 1.
Thus, 321 is a 321-like Number.
Sample Input 2
123
Sample Output 2
No
For N=123, the following holds:
- The first digit from the top, 1, is not greater than the second digit from the top, 2.
Thus, 123 is not a 321-like Number.
Sample Input 3
1
Sample Output 3
Yes
Sample Input 4
86411
Sample Output 4
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君は N 歳の誕生日を迎えました。この時の彼の身長は T cmです。
また、以下のことが分かっています。
- 高橋君は 0 歳の誕生日(生まれた当日)から X 歳の誕生日までの間、毎年身長が D cmずつ伸びた。より厳密に書くと、i=1,2,\ldots,X それぞれに対し、i-1 歳の誕生日から i 歳の誕生日までの間に身長が D cm伸びた。
- 高橋君は X 歳の誕生日から N 歳の誕生日までの間、身長が変化していない。
高橋君の M 歳の誕生日の時の身長が何cmだったかを求めてください。
制約
- 0 \leq M \lt N \leq 100
- 1 \leq X \leq N
- 1 \leq T \leq 200
- 1 \leq D \leq 100
- 高橋君の 0 歳の誕生日の時の身長は 1 cm以上である
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M X T D
出力
答えを整数として出力せよ。
入力例 1
38 20 17 168 3
出力例 1
168
この例では、高橋君の 38 歳の誕生日の時の身長が 168 cmです。また、17 歳の誕生日から 38 歳の誕生日までの間、身長が変化していません。
このことから、20 歳の誕生日の時の身長は 168 cmだったと言え、これが答えになります。
入力例 2
1 0 1 3 2
出力例 2
1
この例において、高橋君は 0(=M) 歳の誕生日の時の身長が 1 cmで、1(=N) 歳の誕生日の時の身長が 3(=T) cmです。
入力例 3
100 10 100 180 1
出力例 3
90
Score : 100 points
Problem Statement
Takahashi had his N-th birthday, when he was T centimeters tall.
Additionally, we know the following facts:
- In each year between Takahashi's birth (0-th birthday) and his X-th birthday, his height increased by D centimeters. More formally, for each i = 1, 2, \ldots, X, his height increased by D centimeters between his (i-1)-th birthday and his i-th birthday.
- Between Takahashi's X-th birthday and his N-th birthday, his height did not change.
Find Takahashi's height on his M-th birthday, in centimeters.
Constraints
- 0 \leq M \lt N \leq 100
- 1 \leq X \leq N
- 1 \leq T \leq 200
- 1 \leq D \leq 100
- Takahashi was at least 1 centimeter tall at his birth.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M X T D
Output
Print the answer as an integer.
Sample Input 1
38 20 17 168 3
Sample Output 1
168
In this sample, Takahashi was 168 centimeters tall on his 38-th birthday. Also, his height did not change between his 17-th birthday and 38-th birthday.
From these facts, we find that he was 168 centimeters tall on his 20-th birthday, so the answer is 168.
Sample Input 2
1 0 1 3 2
Sample Output 2
1
In this sample, Takahashi was 1 centimeter tall on his 0(=M)-th birthday and 3(=T) centimeters tall on his 1(=N)-st birthday.
Sample Input 3
100 10 100 180 1
Sample Output 3
90
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 個の整数 A_1, A_2, \ldots, A_N が与えられます。 このうち最大でない整数の中で最大である整数を求めてください。
ただし、この問題の制約下で答えは必ず存在します。
制約
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- A_1, A_2, \ldots, A_N がすべて等しいということはない
- 入力値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 2 1 3 3 2
出力例 1
2
2,1,3,3,2 の中で最大である整数は 3 です。
2,1,3,3,2 の中で 3 でない整数は 2,1,2 の 3 つであり、これらの中で最大である整数は 2 です。
入力例 2
4 4 3 2 1
出力例 2
3
入力例 3
8 22 22 18 16 22 18 18 22
出力例 3
18
Score : 200 points
Problem Statement
You are given N integers A_1, A_2, \ldots, A_N. Find the largest among those integers that are not the largest.
The constraints of this problem guarantee that the answer exists.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- It is not the case that all A_1, A_2, \ldots, A_N are equal.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
5 2 1 3 3 2
Sample Output 1
2
The largest integer among 2,1,3,3,2 is 3.
The integers that are not 3 among 2,1,3,3,2 are 2,1,2, among which the largest is 2.
Sample Input 2
4 4 3 2 1
Sample Output 2
3
Sample Input 3
8 22 22 18 16 22 18 18 22
Sample Output 3
18
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
xy 平面を考えます。x 軸の正の向きを東向き、y 軸の正の向きを北向きとします。
高橋君ははじめ、点 (x, y) = (0, 0) にいて東( x 軸の正の向き)を向いています。
S と R のみからなる長さ N の文字列 T = t_1t_2\ldots t_N が与えられます。
高橋君は i = 1, 2, \ldots, N の順番で下記を行います。
- t_i =
Sならば、高橋君はいま向いている方向に距離 1 だけ進む。 - t_i =
Rならば、高橋君はその場で右に 90 度回転する。その結果、高橋君の向いている方向が下記の通りに変わる。- 回転前の向きが東向き( x 軸の正の向き)ならば、回転後の向きは南向き( y 軸の負の向き)になる。
- 回転前の向きが南向き( y 軸の負の向き)ならば、回転後の向きは西向き( x 軸の負の向き)になる。
- 回転前の向きが西向き( x 軸の負の向き)ならば、回転後の向きは北向き( y 軸の正の向き)になる。
- 回転前の向きが北向き( y 軸の正の向き)ならば、回転後の向きは東向き( x 軸の正の向き)になる。
上記の手順を終えた後に高橋君がいる点の座標を出力してください。
制約
- 1 \leq N \leq 10^5
- N は整数
- T は
SとRのみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T
出力
問題文中の手順を終えた後に高橋君がいる点の座標 (x, y) を、下記の形式にしたがって空白区切りで出力せよ。
x y
入力例 1
4 SSRS
出力例 1
2 -1
高橋君ははじめ (0, 0) にいて東を向いています。その後、高橋君は下記の通りに行動します。
- t_1 =
Sであるので、高橋君は東に距離 1 だけ進んだ (1, 0) に移動します。 - t_2 =
Sであるので、高橋君は東に距離 1 だけ進んだ (2, 0) に移動します。 - t_3 =
Rであるので、高橋君は右に 90 度回転し、高橋君は南を向きます。 - t_4 =
Sであるので、高橋君は南に距離 1 だけ進んだ (2, -1) に移動します。
よって、高橋君の最終的な位置である (x, y) = (2, -1) を出力します。
入力例 2
20 SRSRSSRSSSRSRRRRRSRR
出力例 2
0 1
Score : 200 points
Problem Statement
Consider an xy-plane. The positive direction of the x-axis is in the direction of east, and the positive direction of the y-axis is in the direction of north.
Takahashi is initially at point (x, y) = (0, 0) and facing east (in the positive direction of the x-axis).
You are given a string T = t_1t_2\ldots t_N of length N consisting of S and R.
Takahashi will do the following move for each i = 1, 2, \ldots, N in this order.
- If t_i =
S, Takahashi advances in the current direction by distance 1. - If t_i =
R, Takahashi turns 90 degrees clockwise without changing his position. As a result, Takahashi's direction changes as follows.- If he is facing east (in the positive direction of the x-axis) before he turns, he will face south (in the negative direction of the y-axis) after he turns.
- If he is facing south (in the negative direction of the y-axis) before he turns, he will face west (in the negative direction of the x-axis) after he turns.
- If he is facing west (in the negative direction of the x-axis) before he turns, he will face north (in the positive direction of the y-axis) after he turns.
- If he is facing north (in the positive direction of the y-axis) before he turns, he will face east (in the positive direction of the x-axis) after he turns.
Print the coordinates Takahashi is at after all the steps above have been done.
Constraints
- 1 \leq N \leq 10^5
- N is an integer.
- T is a string of length N consisting of
SandR.
Input
Input is given from Standard Input in the following format:
N T
Output
Print the coordinates (x, y) Takahashi is at after all the steps described in the Problem Statement have been completed, in the following format, with a space in between:
x y
Sample Input 1
4 SSRS
Sample Output 1
2 -1
Takahashi is initially at (0, 0) facing east. Then, he moves as follows.
- t_1 =
S, so he advances in the direction of east by distance 1, arriving at (1, 0). - t_2 =
S, so he advances in the direction of east by distance 1, arriving at (2, 0). - t_3 =
R, so he turns 90 degrees clockwise, resulting in facing south. - t_4 =
S, so he advances in the direction of south by distance 1, arriving at (2, -1).
Thus, Takahashi's final position, (x, y) = (2, -1), should be printed.
Sample Input 2
20 SRSRSSRSSSRSRRRRRSRR
Sample Output 2
0 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 人の生徒からなるクラスがあり、i\,(1 \leq i \leq N) 番目の生徒の身長は A_i です。
j=1,2,\ldots,Q について、以下の質問に答えてください。
- N 人のうち、身長が x_j 以上の生徒は何人か?
制約
- 1 \leq N,Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq x_j \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q A_1 A_2 \ldots A_N x_1 x_2 \vdots x_Q
出力
Q 行出力せよ。
j\,(1 \leq j \leq Q) 行目には身長が x_j 以上の生徒の数を出力せよ。
入力例 1
3 1 100 160 130 120
出力例 1
2
身長が 120 以上の生徒は 2 番目の生徒と 3 番目の生徒です。
入力例 2
5 5 1 2 3 4 5 6 5 4 3 2
出力例 2
0 1 2 3 4
入力例 3
5 5 804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 189641422
出力例 3
5 3 5 5 5
Score : 300 points
Problem Statement
There is a class with N students. The height of the i-th student (1 \leq i \leq N) is A_i.
For each j=1,2,\ldots,Q, answer the following question.
- How many of the N students have a height of at least x_j?
Constraints
- 1 \leq N,Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq x_j \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q A_1 A_2 \ldots A_N x_1 x_2 \vdots x_Q
Output
Print Q lines.
The j-th line (1 \leq j \leq Q) should contain the number of students with a height of at least x_j.
Sample Input 1
3 1 100 160 130 120
Sample Output 1
2
The students with a height of at least 120 are the 2-nd and 3-rd ones.
Sample Input 2
5 5 1 2 3 4 5 6 5 4 3 2
Sample Output 2
0 1 2 3 4
Sample Input 3
5 5 804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 189641422
Sample Output 3
5 3 5 5 5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
整数 N が与えられます。N の各桁の数字を取り出して並べ(並べる順序は好きに変えてよい)、2 つの正整数に分離することを考えましょう。
例えば、123 という整数に対しては以下の 6 通りの分離の仕方が考えられます。
- 12 と 3
- 21 と 3
- 13 と 2
- 31 と 2
- 23 と 1
- 32 と 1
なお、ここで分離されたあとの 2 整数に leading zero が含まれていてはなりません。例えば、101 という整数を 1 と 01 の 2 つに分離することはできません。また上述の「正整数に分離する」という条件より、101 を 11 と 0 の 2 つに分離することもできません。
適切に N を分離したとき、分離後の 2 数の積の最大値はいくらになりますか?
制約
- N は 1 以上 10^9 以下の整数
- N には 0 でない桁が 2 つ以上含まれる
入力
入力は以下の形式で標準入力から与えられる。
N
出力
分離後の 2 数の積の最大値を出力せよ。
入力例 1
123
出力例 1
63
問題文中にある通り、以下の 6 通りの分離の仕方が考えられます。
- 12 と 3
- 21 と 3
- 13 と 2
- 31 と 2
- 23 と 1
- 32 と 1
積はそれぞれ 36, 63, 26, 62, 23, 32 であり、この中の最大値は 63 です。
入力例 2
1010
出力例 2
100
考えられる分離の仕方は以下の 2 通りです。
- 100 と 1
- 10 と 10
いずれの場合にも積は 100 となります。
入力例 3
998244353
出力例 3
939337176
Score : 300 points
Problem Statement
You are given an integer N. Consider permuting the digits in N and separate them into two positive integers.
For example, for the integer 123, there are six ways to separate it, as follows:
- 12 and 3,
- 21 and 3,
- 13 and 2,
- 31 and 2,
- 23 and 1,
- 32 and 1.
Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer 101 into 1 and 01. Additionally, since the resulting integers must be positive, it is not allowed to separate 101 into 11 and 0, either.
What is the maximum possible product of the two resulting integers, obtained by the optimal separation?
Constraints
- N is an integer between 1 and 10^9 (inclusive).
- N contains two or more digits that are not 0.
Input
Input is given from Standard Input in the following format:
N
Output
Print the maximum possible product of the two integers after separation.
Sample Input 1
123
Sample Output 1
63
As described in Problem Statement, there are six ways to separate it:
- 12 and 3,
- 21 and 3,
- 13 and 2,
- 31 and 2,
- 23 and 1,
- 32 and 1.
The products of these pairs, in this order, are 36, 63, 26, 62, 23, 32, with 63 being the maximum.
Sample Input 2
1010
Sample Output 2
100
There are two ways to separate it:
- 100 and 1,
- 10 and 10.
In either case, the product is 100.
Sample Input 3
998244353
Sample Output 3
939337176
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の整数列 A = (A_1, \dots, A_N) が与えられます。
以下の条件を全て満たす整数の組 (i, j, k) の総数を求めてください。
- 1 \leq i, j, k \leq N
- \frac{A_i}{A_j} = A_k
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 6 2 3
出力例 1
2
(i, j, k) = (1, 2, 3), (1, 3, 2) が条件を満たします。
入力例 2
1 2
出力例 2
0
入力例 3
10 1 3 2 4 6 8 2 2 3 7
出力例 3
62
Score : 400 points
Problem Statement
You are given an integer sequence A = (A_1, \dots, A_N) of length N.
Find the number of triplets of integers (i, j, k) satisfying all of the conditions below.
- 1 \leq i, j, k \leq N
- \frac{A_i}{A_j} = A_k
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 6 2 3
Sample Output 1
2
(i, j, k) = (1, 2, 3), (1, 3, 2) satisfy the conditions.
Sample Input 2
1 2
Sample Output 2
0
Sample Input 3
10 1 3 2 4 6 8 2 2 3 7
Sample Output 3
62
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純有向グラフが与えられます。辺 i は頂点 u_i から頂点 v_i への有向辺です。
また、あなたは次の操作を 0 回以上何度でも行えます。
- 相異なる頂点 x,y であって頂点 x から頂点 y への有向辺が存在しないようなものを選ぶ。そして、頂点 x から頂点 y への有向辺を追加する。
このグラフが次の条件を満たす状態にするために最小で何回操作を行う必要があるかを求めてください。
- 相異なる頂点 a,b,c すべてについて、頂点 a から頂点 b への有向辺と頂点 b から頂点 c への有向辺がともに存在するならば頂点 a から頂点 c への有向辺も存在する。
制約
- 3 \leq N \leq 2000
- 0 \leq M \leq 2000
- 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)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
4 3 2 4 3 1 4 3
出力例 1
3
初め、一例として頂点 2,4,3 について、頂点 2 から頂点 4 への有向辺と頂点 4 から頂点 3 への有向辺がともに存在するにもかかわらず、頂点 2 から頂点 3 への有向辺は存在せず、条件を満たさない状態です。
そこで、以下の 3 本の有向辺を追加すると条件を満たす状態になります。
- 頂点 2 から頂点 3 への有向辺
- 頂点 2 から頂点 1 への有向辺
- 頂点 4 から頂点 1 への有向辺
一方、3 本未満の追加で条件を満たす状態には出来ないため、答えは 3 です。
入力例 2
292 0
出力例 2
0
入力例 3
5 8 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1
出力例 3
12
Score : 500 points
Problem Statement
You are given a simple directed graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i is a directed edge from vertex u_i to vertex v_i.
You may perform the following operation zero or more times.
- Choose distinct vertices x and y such that there is no directed edge from vertex x to vertex y, and add a directed edge from vertex x to vertex y.
Find the minimum number of times you need to perform the operation to make the graph satisfy the following condition.
- For every triple of distinct vertices a, b, and c, if there are directed edges from vertex a to vertex b and from vertex b to vertex c, there is also a directed edge from vertex a to vertex c.
Constraints
- 3 \leq N \leq 2000
- 0 \leq M \leq 2000
- 1 \leq u_i ,v_i \leq N
- u_i \neq v_i
- (u_i,v_i) \neq (u_j,v_j) if i \neq j.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
4 3 2 4 3 1 4 3
Sample Output 1
3
Initially, the condition is not satisfied because, for instance, for vertices 2, 4, and 3, there are directed edges from vertex 2 to vertex 4 and from vertex 4 to vertex 3, but not from vertex 2 to vertex 3.
You can make the graph satisfy the condition by adding the following three directed edges:
- one from vertex 2 to vertex 3,
- one from vertex 2 to vertex 1, and
- one from vertex 4 to vertex 1.
On the other hand, the condition cannot be satisfied by adding two or fewer edges, so the answer is 3.
Sample Input 2
292 0
Sample Output 2
0
Sample Input 3
5 8 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1
Sample Output 3
12
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
黒板に 1 以上 M 以下の整数からなる集合 N 個 S_1,S_2,\dots,S_N が書かれています。ここで、S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace です。
あなたは、以下の操作を好きな回数(0 回でもよい)行うことが出来ます。
- 1 個以上の共通した要素を持つ 2 個の集合 X,Y を選ぶ。X,Y の 2 個を黒板から消し、新たに X\cup Y を黒板に書く。
ここで、X\cup Y とは X か Y の少なくともどちらかに含まれている要素のみからなる集合を意味します。
1 と M が両方含まれる集合を作ることが出来るか判定してください。出来るならば、必要な操作回数の最小値を求めてください。
制約
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
出力
1 と M が両方含まれる集合を作ることが出来るならば必要な操作回数の最小値を、出来ないならば -1 を出力せよ。
入力例 1
3 5 2 1 2 2 2 3 3 3 4 5
出力例 1
2
まず、\lbrace 1,2 \rbrace と \lbrace 2,3 \rbrace を選んで消し、\lbrace 1,2,3 \rbrace を追加します。
そして、\lbrace 1,2,3 \rbrace と \lbrace 3,4,5 \rbrace を選んで消し、\lbrace 1,2,3,4,5 \rbrace を追加します。
すると 2 回の操作で 1 と M を両方含む集合を作ることが出来ます。1 回の操作では目標を達成できないため、答えは 2 です。
入力例 2
1 2 2 1 2
出力例 2
0
始めから S_1 が 1,M を共に含むため、必要な操作回数の最小値は 0 回です。
入力例 3
3 5 2 1 3 2 2 4 3 2 4 5
出力例 3
-1
入力例 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
出力例 4
2
Score : 500 points
Problem Statement
On a blackboard, there are N sets S_1,S_2,\dots,S_N consisting of integers between 1 and M. Here, S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace.
You may perform the following operation any number of times (possibly zero):
- choose two sets X and Y with at least one common element. Erase them from the blackboard, and write X\cup Y on the blackboard instead.
Here, X\cup Y denotes the set consisting of the elements contained in at least one of X and Y.
Determine if one can obtain a set containing both 1 and M. If it is possible, find the minimum number of operations required to obtain it.
Constraints
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
Output
If one can obtain a set containing both 1 and M, print the minimum number of operations required to obtain it; if it is impossible, print -1 instead.
Sample Input 1
3 5 2 1 2 2 2 3 3 3 4 5
Sample Output 1
2
First, choose and remove \lbrace 1,2 \rbrace and \lbrace 2,3 \rbrace to obtain \lbrace 1,2,3 \rbrace.
Then, choose and remove \lbrace 1,2,3 \rbrace and \lbrace 3,4,5 \rbrace to obtain \lbrace 1,2,3,4,5 \rbrace.
Thus, one can obtain a set containing both 1 and M with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is 2.
Sample Input 2
1 2 2 1 2
Sample Output 2
0
S_1 already contains both 1 and M, so the minimum number of operations required is 0.
Sample Input 3
3 5 2 1 3 2 2 4 3 2 4 5
Sample Output 3
-1
Sample Input 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
Sample Output 4
2