実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
英小文字からなる文字列 S が与えられます。
S に a
が現れるならば最後に現れるのが何文字目かを出力し、現れないならば -1 を出力してください。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
abcdaxayz
出力例 1
7
S に a
は 3 回現れますが、最後に現れるのは 7 文字目なので、7 を出力します。
入力例 2
bcbbbz
出力例 2
-1
S に a
は現れないので、-1 を出力します。
入力例 3
aaaaa
出力例 3
5
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
If a
appears in S, print the last index at which it appears; otherwise, print -1. (The index starts at 1.)
Constraints
- S is a string of length between 1 and 100 (inclusive) consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
abcdaxayz
Sample Output 1
7
a
appears three times in S. The last occurrence is at index 7, so you should print 7.
Sample Input 2
bcbbbz
Sample Output 2
-1
a
does not appear in S, so you should print -1.
Sample Input 3
aaaaa
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
以下のような、1 から 9 までの数字が書かれた 3 \times 3 の盤面があります。
1 以上 9 以下の整数 A,B が与えられます。ただし、A < B です。
A が書かれたマスと B が書かれたマスが左右に隣接しているか判定してください。
制約
- 1 \le A < B \le 9
- A, B は整数である。
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
A が書かれたマスと B が書かれたマスが左右に隣接しているならば Yes
、そうでないならば No
を出力せよ。
入力例 1
7 8
出力例 1
Yes
7 が書かれたマスと 8 が書かれたマスは左右に隣り合っているので、Yes
を出力します。
入力例 2
1 9
出力例 2
No
入力例 3
3 4
出力例 3
No
Score : 100 points
Problem Statement
We have the following 3 \times 3 board with integers from 1 through 9 written on it.
You are given two integers A and B between 1 and 9, where A < B.
Determine if the two squares with A and B written on them are adjacent horizontally.
Constraints
- 1 \le A < B \le 9
- A and B are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print Yes
if the two squares with A and B written on them are adjacent horizontally, and No
otherwise.
Sample Input 1
7 8
Sample Output 1
Yes
The two squares with 7 and 8 written on them are adjacent horizontally, so print Yes
.
Sample Input 2
1 9
Sample Output 2
No
Sample Input 3
3 4
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
以下の図で表される正五角形 P があります。
P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しいか判定してください。
制約
- S_1,S_2,T_1,T_2 は
A
,B
,C
,D
,E
のいずれかの文字 - S_1 \neq S_2
- T_1 \neq T_2
入力
入力は以下の形式で標準入力から与えられる。
S_1S_2 T_1T_2
出力
P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しい場合 Yes
を、等しくない場合 No
を出力せよ。
入力例 1
AC EC
出力例 1
Yes
P の点 A
と点 C
を結ぶ線分と、P の点 E
と点 C
を結ぶ線分の長さは等しいです。
入力例 2
DA EA
出力例 2
No
P の点 D
と点 A
を結ぶ線分と、P の点 E
と点 A
を結ぶ線分の長さは等しくありません。
入力例 3
BD BD
出力例 3
Yes
Score : 200 points
Problem Statement
A regular pentagon P is shown in the figure below.
Determine whether the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2.
Constraints
- Each of S_1, S_2, T_1, and T_2 is one of the characters
A
,B
,C
,D
, andE
. - S_1 \neq S_2
- T_1 \neq T_2
Input
The input is given from Standard Input in the following format:
S_1S_2 T_1T_2
Output
If the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2, print Yes
; otherwise, print No
.
Sample Input 1
AC EC
Sample Output 1
Yes
The length of the line segment connecting point A
and point C
of P equals the length of the line segment connecting point E
and point C
.
Sample Input 2
DA EA
Sample Output 2
No
The length of the line segment connecting point D
and point A
of P does not equal the length of the line segment connecting point E
and point A
.
Sample Input 3
BD BD
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S が良い文字列であるとは、すべての 1 以上の整数 i について次の性質が成り立つことであるとします。
- S にちょうど i 回現れる文字はちょうど 0 種類またはちょうど 2 種類ある
文字列 S が与えられるので、 S が良い文字列か判定してください。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が良い文字列ならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
commencement
出力例 1
Yes
文字列 commencement
にちょうど i 回現れる文字の種類数は以下のとおりです。
- i=1:
o
,t
の 2 種類 - i=2:
c
,n
の 2 種類 - i=3:
e
,m
の 2 種類 - i\geq 4: 0 種類
よって、commencement
は良い文字列の条件を満たします。
入力例 2
banana
出力例 2
No
文字列 banana
にちょうど 1 回現れる文字は b
の 1 種類だけであり、良い文字列の条件を満たしません。
入力例 3
ab
出力例 3
Yes
Score: 200 points
Problem Statement
A string S consisting of lowercase English letters is a good string if and only if it satisfies the following property for all integers i not less than 1:
- There are exactly zero or exactly two different letters that appear exactly i times in S.
Given a string S, determine if it is a good string.
Constraints
- S is a string of lowercase English letters with a length between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes
if S is a good string, and No
otherwise.
Sample Input 1
commencement
Sample Output 1
Yes
For the string commencement
, the number of different letters that appear exactly i times is as follows:
- i=1: two letters (
o
andt
) - i=2: two letters (
c
andn
) - i=3: two letters (
e
andm
) - i\geq 4: zero letters
Therefore, commencement
satisfies the condition of a good string.
Sample Input 2
banana
Sample Output 2
No
For the string banana
, there is only one letter that appears exactly one time, which is b
, so it does not satisfy the condition of a good string.
Sample Input 3
ab
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。
ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。
単純パスとは?
グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_i と v_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。制約
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- 入力はすべて整数
- 与えられるグラフは木
入力
入力は以下の形式で標準入力から与えられる。
N X Y U_1 V_1 U_2 V_2 \vdots U_{N-1} V_{N-1}
出力
頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。
入力例 1
5 2 5 1 2 1 3 3 4 3 5
出力例 1
2 1 3 5
木 T は以下のような形であり、頂点 2 から頂点 5への単純パスは
頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。
入力例 2
6 1 2 3 1 2 5 1 2 4 1 2 6
出力例 2
1 2
木 T は以下のような形です。
Score : 300 points
Problem Statement
There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.
You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.
It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.
What is a simple path?
For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- All values in the input are integers.
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N X Y U_1 V_1 U_2 V_2 \vdots U_{N-1} V_{N-1}
Output
Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.
Sample Input 1
5 2 5 1 2 1 3 3 4 3 5
Sample Output 1
2 1 3 5
The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.
Sample Input 2
6 1 2 3 1 2 5 1 2 4 1 2 6
Sample Output 2
1 2
The tree T is shown below.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
N 人のプレイヤーが参加するプログラミングコンテスト World Tour Finals が行われており、競技時間の半分が過ぎました。 このコンテストでは M 問の問題が出題されており、問題 i の点数 A_i は 500 以上 2500 以下の 100 の倍数です。
各 i = 1, \ldots, N について、プレイヤー i がどの問題を既に解いたかを表す文字列 S_i が与えられます。
S_i は o
, x
からなる長さ M の文字列で、S_i の j 文字目が o
のときプレイヤー i は問題 j を既に解いており、x
のときまだ解いていません。
ただし、どのプレイヤーもまだ全ての問題を解いてはいません。
プレイヤー i の総合得点は、解いた問題の点数の合計に、ボーナス点 i 点を加えた点数として計算されます。
さて、各 i = 1, \ldots, N について以下の質問に答えてください。
- プレイヤー i がまだ解いていない問題を少なくとも何問解くことで、プレイヤー i の総合得点が他のプレイヤー全員の現在の総合得点を上回ることができますか?
なお、問題文中の条件と制約から、プレイヤー i が全ての問題を解くことで、他のプレイヤー全員の現在の総合得点を上回ることができることが証明できます。 このことから、答えは常に定義されることに注意してください。
制約
- 2\leq N\leq 100
- 1\leq M\leq 100
- 500\leq A_i\leq 2500
- A_i は 100 の倍数
- S_i は
o
,x
からなる長さ M の文字列 - S_i には
x
が一個以上含まれる - 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_M S_1 S_2 \vdots S_N
出力
N 行出力せよ。 i 行目にはプレイヤー i に関する質問の答えを出力せよ。
入力例 1
3 4 1000 500 700 2000 xxxo ooxx oxox
出力例 1
0 1 1
競技時間の半分の経過時の各プレイヤーの総合得点は、プレイヤー 1 が 2001 点、プレイヤー 2 が 1502 点、プレイヤー 3 が 1703 点です。
プレイヤー 1 は 1 問も解かずとも、他のプレイヤー全員の総合得点を上回っています。
プレイヤー 2 は、例えば問題 4 を解けば総合得点が 3502 点となり、他のプレイヤー全員の総合得点を上回ります。
プレイヤー 3 も、例えば問題 4 を解けば総合得点が 3703 点となり、他のプレイヤー全員の総合得点を上回ります。
入力例 2
5 5 1000 1500 2000 2000 2500 xxxxx oxxxx xxxxx oxxxx oxxxx
出力例 2
1 1 1 1 0
入力例 3
7 8 500 500 500 500 500 500 500 500 xxxxxxxx oxxxxxxx ooxxxxxx oooxxxxx ooooxxxx oooooxxx ooooooxx
出力例 3
7 6 5 4 3 2 0
Score : 250 points
Problem Statement
The programming contest World Tour Finals is underway, where N players are participating, and half of the competition time has passed. There are M problems in this contest, and the score A_i of problem i is a multiple of 100 between 500 and 2500, inclusive.
For each i = 1, \ldots, N, you are given a string S_i that indicates which problems player i has already solved.
S_i is a string of length M consisting of o
and x
, where the j-th character of S_i is o
if player i has already solved problem j, and x
if they have not yet solved it.
Here, none of the players have solved all the problems yet.
The total score of player i is calculated as the sum of the scores of the problems they have solved, plus a bonus score of i points.
For each i = 1, \ldots, N, answer the following question.
- At least how many of the problems that player i has not yet solved must player i solve to exceed all other players' current total scores?
Note that under the conditions in this statement and the constraints, it can be proved that player i can exceed all other players' current total scores by solving all the problems, so the answer is always defined.
Constraints
- 2\leq N\leq 100
- 1\leq M\leq 100
- 500\leq A_i\leq 2500
- A_i is a multiple of 100.
- S_i is a string of length M consisting of
o
andx
. - S_i contains at least one
x
. - All numeric values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_M S_1 S_2 \vdots S_N
Output
Print N lines. The i-th line should contain the answer to the question for player i.
Sample Input 1
3 4 1000 500 700 2000 xxxo ooxx oxox
Sample Output 1
0 1 1
The players' total scores at the halfway point of the competition time are 2001 points for player 1, 1502 points for player 2, and 1703 points for player 3.
Player 1 is already ahead of all other players' total scores without solving any more problems.
Player 2 can, for example, solve problem 4 to have a total score of 3502 points, which would exceed all other players' total scores.
Player 3 can also, for example, solve problem 4 to have a total score of 3703 points, which would exceed all other players' total scores.
Sample Input 2
5 5 1000 1500 2000 2000 2500 xxxxx oxxxx xxxxx oxxxx oxxxx
Sample Output 2
1 1 1 1 0
Sample Input 3
7 8 500 500 500 500 500 500 500 500 xxxxxxxx oxxxxxxx ooxxxxxx oooxxxxx ooooxxxx oooooxxx ooooooxx
Sample Output 3
7 6 5 4 3 2 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
AtCoder 国は無限に広がる直交座標の上にあります。
AtCoder 国には N 個の街があり、 1,2,\dots,N と番号が付けられています。街 i は地点 (x_i, y_i) にあり、2 つの異なる番号の街が同じ座標にあることはありません。
AtCoder 国には転移魔法(以下、魔法と表記)があります。魔法は整数の組 (a,b) によって識別されていて、地点 (x,y) にいるときに魔法 (a,b) を使うと (x+a,y+b) にワープすることができます。
すぬけ君は、任意の整数の組 (a,b) を選んで魔法 (a,b) を覚えることができる大魔術師です。また、すぬけ君は何種類でも魔法を覚えることができます。
魔法を使って街と街の間を移動したくなったすぬけ君は、全ての相異なる街の組 (i,j) について次の行動を取れるようにいくつかの魔法を覚えることにしました。
- 覚えた魔法のうち 1 種類の魔法のみ を選ぶ。その後、選んだ魔法 のみ を繰り返し使って街 i から 街 j に移動する。
すぬけ君が上の条件を満たすように魔法を覚えるとき、少なくとも何種類の魔法を覚えればよいですか?
制約
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- i \neq j ならば (x_i, y_i) \neq (x_j, y_j) である。
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 x_2 y_2 \vdots x_N y_N
出力
すぬけ君が覚える必要がある魔法の個数の最小値を出力せよ。
入力例 1
3 1 2 3 6 7 4
出力例 1
6
AtCoder 国の街の位置を図示したのが下の図です。(わかりやすさのために四隅の座標を表示しています。)
すぬけ君は次の 6 種類の魔法を覚えれば、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 1 回使うことで街 j に着くことができるので条件を満たします。
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
次の 6 種類の魔法も条件を満たします。このときすぬけ君は、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 2 回使うことで街 j に着くことができます。
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
条件を満たす魔法の組み合わせのうち 6 種類より少ないものは存在しないので、 6 を出力します。
入力例 2
3 1 2 2 2 4 2
出力例 2
2
次の 2 種類の魔法を覚えるのが最適です。
- (1, 0)
- (-1, 0)
入力例 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
出力例 3
8
Score : 400 points
Problem Statement
The Republic of AtCoder lies on a Cartesian coordinate plane.
It has N towns, numbered 1,2,\dots,N. Town i is at (x_i, y_i), and no two different towns are at the same coordinates.
There are teleportation spells in the nation. A spell is identified by a pair of integers (a,b), and casting a spell (a, b) at coordinates (x, y) teleports you to (x+a, y+b).
Snuke is a great magician who can learn the spell (a, b) for any pair of integers (a, b) of his choice. The number of spells he can learn is also unlimited.
To be able to travel between the towns using spells, he has decided to learn some number of spells so that it is possible to do the following for every pair of different towns (i, j).
- Choose just one spell among the spells learned. Then, repeatedly use just the chosen spell to get from Town i to Town j.
At least how many spells does Snuke need to learn to achieve the objective above?
Constraints
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- (x_i, y_i) \neq (x_j, y_j) if i \neq j.
Input
Input is given from Standard Input in the following format:
N x_1 y_1 x_2 y_2 \vdots x_N y_N
Output
Print the minimum number of spells Snuke needs to learn.
Sample Input 1
3 1 2 3 6 7 4
Sample Output 1
6
The figure below illustrates the positions of the towns (along with the coordinates of the four corners).
If Snuke learns the six spells below, he can get from Town i to Town j by using one of the spells once for every pair (i,j) (i \neq j), achieving his objective.
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
Another option is to learn the six spells below. In this case, he can get from Town i to Town j by using one of the spells twice for every pair (i,j) (i \neq j), achieving his objective.
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
There is no combination of spells that consists of less than six spells and achieve the objective, so we should print 6.
Sample Input 2
3 1 2 2 2 4 2
Sample Output 2
2
The optimal choice is to learn the two spells below:
- (1, 0)
- (-1, 0)
Sample Input 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
Sample Output 3
8
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
英小文字のみからなる文字列 X に対し、以下の手続きによって文字列を得ることを考えます。
- X を異なる文字が隣り合っている部分で分割する。
- 分割した各文字列 Y に対して、Y を Y を構成する文字と Y の長さを繋げた文字列に置き換える。
- 元の順番を保ったまま、置き換えた文字列をすべて繋げる。
例えば、aaabbcccc
の場合、aaa
,bb
,cccc
に分けられ、それぞれを a3
,b2
,c4
に置き換え、その順番のまま繋げることにより a3b2c4
を得ます。また、aaaaaaaaaa
の場合、a10
になります。
長さ N の英小文字のみからなる文字列 S のうち、上記の手続きによって得られた文字列 T との長さを比べたとき、T の方が短いものの個数を P で割ったあまりを求めてください。
制約
- 1 \le N \le 3000
- 10^8 \le P \le 10^9
- N,P は整数
- P は素数
入力
入力は以下の形式で標準入力から与えられる。
N P
出力
答えを出力せよ。
入力例 1
3 998244353
出力例 1
26
1,2,3 文字目が全て等しい文字列のみが条件を満たします。
例えば、aaa
は a3
となり条件を満たしますが、abc
は a1b1c1
となり条件を満たしません。
入力例 2
2 998244353
出力例 2
0
aa
→ a2
のように、長さが等しいものは条件を満たさないことに注意してください。
入力例 3
5 998244353
出力例 3
2626
aaabb
や aaaaa
などが条件を満たします。
入力例 4
3000 924844033
出力例 4
607425699
条件を満たす文字列の個数を P で割ったあまりを求めてください。
Score : 500 points
Problem Statement
Consider the following procedure of, given a string X consisting of lowercase English alphabets, obtaining a new string:
- Split the string X off at the positions where two different characters are adjacent to each other.
- For each string Y that has been split off, replace Y with a string consisting of the character which Y consists of, followed by the length of Y.
- Concatenate the replaced strings without changing the order.
For example, aaabbcccc
is divided into aaa
,bb
,cccc
, which are replaced by a3
,b2
,c4
, respectively, which in turn are concatenated without changing the order, resulting in a3b2c4
.If the given string is aaaaaaaaaa
, the new string is a10
.
Find the number, modulo P, of strings S of lengths N consisting of lowercase English alphabets, such that the length of T is smaller than that of S, where T is the string obtained by the procedure above against the string S.
Constraints
- 1 \le N \le 3000
- 10^8 \le P \le 10^9
- N and P are integers.
- P is a prime.
Input
Input is given from Standard Input in the following format:
N P
Output
Print the answer.
Sample Input 1
3 998244353
Sample Output 1
26
Those strings of which the 1-st, 2-nd, and 3-rd characters are all the same satisfy the condition.
For example, aaa
becomes a3
, which satisfies the condition, while abc
becomes a1b1c1
, which does not.
Sample Input 2
2 998244353
Sample Output 2
0
Note that if a string is transformed into another string of the same length, such as aa
that becomes a2
, it does not satisfy the condition.
Sample Input 3
5 998244353
Sample Output 3
2626
Strings like aaabb
and aaaaa
satisfy the condition.
Sample Input 4
3000 924844033
Sample Output 4
607425699
Find the number of strings satisfying the condition modulo P.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
N 個の頂点と M 本の辺からなる連結かつ単純な無向グラフがあります。 頂点には 1 から N までの整数の番号がついています。
あなたは、はじめ頂点 1 にいます。 隣り合う頂点に移動することを最大 2N 回まで繰り返して、頂点 N へ移動してください。
ただし、はじめはグラフの辺をすべて知ることはできず、今いる頂点と隣り合っている頂点の情報を知ることができます。
制約
- 2\leq N\leq100
- N-1\leq M\leq\dfrac{N(N-1)}2
- グラフは連結かつ単純
- 入力はすべて整数
入出力
最初に、グラフの頂点数 N と辺数 M を標準入力から受け取ってください。
N M
次に、あなたはジャッジに対して問題文中の操作を 2N 回まで繰り返すことができます。
各操作のはじめには、あなたが現在いる頂点に隣接する頂点が次の形式で標準入力から与えられます。
k v _ 1 v _ 2 \ldots v _ k
ここで、v _ i\ (1\leq i\leq k) は 1 以上 N 以下の整数で、v _ 1\lt v _ 2\lt\cdots\lt v _ k を満たします。
あなたは、v _ i\ (1\leq i\leq k) を 1 つ選んで以下の形式で標準出力へ出力してください。
v _ i
この操作をすることで、あなたは頂点 v _ i へ移動します。
移動回数が 2N 回を上回ったり、不正な出力を行った場合、ジャッジは標準入力に -1
を送ります。
移動する先の頂点が頂点 N である場合、ジャッジは標準入力に OK
を送り、終了します。
-1
もしくは OK
を受け取った場合、ただちにあなたのプログラムを終了させてください。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- 頂点 N に到達したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
- この問題のジャッジはアダプティブです。つまり、制約および以前の出力に矛盾しない範囲でグラフの形が変わる場合があります。
入出力例
以下は、N=4,M=5 の場合の入出力例です。 この入出力では、グラフは以下の図のようになっています。
入力 | 出力 | 説明 |
---|---|---|
4 5 |
N と M が与えられます。 | |
2 2 3 |
はじめ、あなたは頂点 1 にいます。頂点 1 に隣接している頂点が与えられます。 | |
3 |
移動する頂点として、頂点 v _ 2=3 を選びます。 | |
3 1 2 4 |
頂点 3 に隣接している頂点が与えられます。 | |
2 |
移動する頂点として、頂点 v _ 2=2 を選びます。 | |
3 1 3 4 |
頂点 2 に隣接している頂点が与えられます。 | |
4 |
移動する頂点として、頂点 v _ 3=4 を選びます。 | |
OK |
8(=2\times4) 回以内で頂点 4 に到達したので、OK が渡されます。 |
OK
を受け取ったあと、ただちにプログラムを終了することで正解と判定されます。
Score : 525 points
Problem Statement
This is an interactive problem (where your program and the judge program interact through Standard Input and Output).
There is a simple connected undirected graph with N vertices and M edges. The vertices are numbered with integers from 1 to N.
Initially, you are at vertex 1. Repeat moving to an adjacent vertex at most 2N times to reach vertex N.
Here, you do not initially know all edges of the graph, but you will be informed of the vertices adjacent to the vertex you are at.
Constraints
- 2\leq N\leq100
- N-1\leq M\leq\dfrac{N(N-1)}2
- The graph is simple and connected.
- All input values are integers.
Input and Output
First, receive the number of vertices N and the number of edges M in the graph from Standard Input:
N M
Next, you get to repeat the operation described in the problem statement at most 2N times against the judge.
At the beginning of each operation, the vertices adjacent to the vertex you are currently at are given from Standard Input in the following format:
k v _ 1 v _ 2 \ldots v _ k
Here, v _ i\ (1\leq i\leq k) are integers between 1 and N such that v _ 1\lt v _ 2\lt\cdots\lt v _ k.
Choose one of v _ i\ (1\leq i\leq k) and print it to Standard Output in the following format:
v _ i
After this operation, you will be at vertex v _ i.
If you perform more than 2N operations or print invalid output, the judge will send -1
to Standard Input.
If the destination of a move is vertex N, the judge will send OK
to Standard Input and terminate.
When receiving -1
or OK
, immediately terminate the program.
Notes
- In each output, insert a newline at the end and flush Standard Output. Otherwise, the verdict may be TLE.
- The verdict will be indeterminate if the program prints invalid output or quits prematurely in the middle of the interaction.
- Terminate the program immediately after reaching vertex N. Otherwise, the verdict will be indeterminate.
- The judge for this problem is adaptive. This means that the graph may change without contradicting the constraints or previous outputs.
Sample Interaction
In the following sample interaction, we have N=4, M=5, and the graph in the figure below.
Input | Output | Description |
---|---|---|
4 5 |
N and M are given. | |
2 2 3 |
You start at vertex 1. The vertices adjacent to vertex 1 are given. | |
3 |
You choose to go to vertex v _ 2=3. | |
3 1 2 4 |
The vertices adjacent to vertex 3 are given. | |
2 |
You choose to go to vertex v _ 2=2. | |
3 1 3 4 |
The vertices adjacent to vertex 2 are given. | |
4 |
You choose to go to vertex v _ 3=4. | |
OK |
Since you have reached vertex 4 within 8(=2\times4) moves, OK is passed. |
You will be judged as correct if you immediately terminate the program after receiving OK
.