Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
AtCoder で定期的に開催されている、国際的な権威があるコンテストである AtCoder Grand Contest (以下、AGC) は今までに 54 回開催されてきました。
みなさんがちょうど参加している 230 回目の ABC である ABC230
と同様に、 当初は N 回目の AGC のコンテスト名には N を 3 桁になるようにゼロ埋めした数が割り振られていました。( 1 回目の AGC は AGC001
, 2 回目の AGC は AGC002
, ...)
ところが、最新の 54 回目の AGC のコンテスト名は AGC055
で、回数より 1 大きい数が割り振られています。これは、AGC042
が社会情勢の影響で中止されて欠番となったため、42 回目以降に開催されたコンテストでは開催された回数より 1 大きい数が割り振られているからです。(入出力例にある説明も参考にしてください。)
さて、ここで問題です。整数 N が与えられるので、N 回目に開催された AGC のコンテスト名を AGCXXX
の形式で出力してください。ここで、XXX
にはゼロ埋めがなされた 3 桁の数が入ります。
制約
- 1 \leq N \leq 54
- N は整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N 回目に開催された AGC のコンテスト名を所定の形式で出力せよ。
入力例 1
42
出力例 1
AGC043
問題文にある通り、 42 回目以降の AGC には回数より 1 大きい数が割り振られています。
よって 42 回目の AGC のコンテスト名は AGC043
になります。
入力例 2
19
出力例 2
AGC019
41 回目以前の AGC は回数と同じ数が割り振られています。
よって AGC019
が答えとなります。
入力例 3
1
出力例 3
AGC001
問題文にある通り、 1 回目の AGC のコンテスト名は AGC001
です。
数が 3 桁になるようにゼロ埋めを行う必要があるのに注意してください。
入力例 4
50
出力例 4
AGC051
Score : 100 points
Problem Statement
AtCoder Grand Contest (AGC), a regularly held contest with a world authority, has been held 54 times.
Just like the 230-th ABC ― the one you are in now ― is called ABC230
, the N-th AGC is initially named with a zero-padded 3-digit number N. (The 1-st AGC is AGC001
, the 2-nd AGC is AGC002
, ...)
However, the latest 54-th AGC is called AGC055
, where the number is one greater than 54. Because AGC042
is canceled and missing due to the social situation, the 42-th and subsequent contests are assigned numbers that are one greater than the numbers of contests held. (See also the explanations at Sample Inputs and Outputs.)
Here is the problem: given an integer N, print the name of the N-th AGC in the format AGCXXX
, where XXX
is the zero-padded 3-digit number.
Constraints
- 1 \leq N \leq 54
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the name of the N-th AGC in the specified format.
Sample Input 1
42
Sample Output 1
AGC043
As explained in Problem Statement, the 42-th and subsequent AGCs are assigned numbers that are one greater than the numbers of contests.
Thus, the 42-th AGC is named AGC043
.
Sample Input 2
19
Sample Output 2
AGC019
The 41-th and preceding AGCs are assigned numbers that are equal to the numbers of contests.
Thus, the answer is AGC019
.
Sample Input 3
1
Sample Output 3
AGC001
As mentioned in Problem Statement, the 1-st AGC is named AGC001
.
Be sure to pad the number with zeros into a 3-digit number.
Sample Input 4
50
Sample Output 4
AGC051
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字 a
, b
, \ldots, z
の ASCII 文字コードはこの順に 97,98,\ldots,122 です。
97 以上 122 以下の整数 N が与えられるので、ASCII 文字コードが N であるような英小文字を出力してください。
制約
- N は 97 以上 122 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
97
出力例 1
a
ASCII 文字コードが 97 である英小文字は a
です。
入力例 2
122
出力例 2
z
Score : 100 points
Problem Statement
The ASCII values of the lowercase English letters a
, b
, \ldots, z
are 97,98,\ldots,122 in this order.
Given an integer N between 97 and 122, print the letter whose ASCII value is N.
Constraints
- N is an integer between 97 and 122 (inclusive).
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
97
Sample Output 1
a
97 is the ASCII value of a
.
Sample Input 2
122
Sample Output 2
z
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
あなたは3Dゲームの当たり判定を実装しようとしています。
3 次元空間内の直方体であって、2 点 (a,b,c),(d,e,f) を結ぶ線分を対角線とし、全ての面が xy 平面、yz 平面、zx 平面のいずれかに平行なものを C(a,b,c,d,e,f) と表します。
(この定義により C(a,b,c,d,e,f) は一意に定まります)
2 つの直方体 C(a,b,c,d,e,f) と C(g,h,i,j,k,l) が与えられるので、これらの共通部分の体積が正かどうか判定してください。
制約
- 0 \leq a < d \leq 1000
- 0 \leq b < e \leq 1000
- 0 \leq c < f \leq 1000
- 0 \leq g < j \leq 1000
- 0 \leq h < k \leq 1000
- 0 \leq i < l \leq 1000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
a b c d e f g h i j k l
出力
2 つの直方体の共通部分の体積が正なら Yes
、そうでなければ No
を出力せよ。
入力例 1
0 0 0 4 5 6 2 3 4 5 6 7
出力例 1
Yes
2 つの直方体の位置関係は下図のようになっており、共通部分の体積は 8 です。
入力例 2
0 0 0 2 2 2 0 0 2 2 2 4
出力例 2
No
2 つの直方体は面で接していますが、共通部分の体積は 0 です。
入力例 3
0 0 0 1000 1000 1000 10 10 10 100 100 100
出力例 3
Yes
Score : 250 points
Problem Statement
You are trying to implement collision detection in a 3D game.
In a 3-dimensional space, let C(a,b,c,d,e,f) denote the cuboid with a diagonal connecting (a,b,c) and (d,e,f), and with all faces parallel to the xy-plane, yz-plane, or zx-plane.
(This definition uniquely determines C(a,b,c,d,e,f).)
Given two cuboids C(a,b,c,d,e,f) and C(g,h,i,j,k,l), determine whether their intersection has a positive volume.
Constraints
- 0 \leq a < d \leq 1000
- 0 \leq b < e \leq 1000
- 0 \leq c < f \leq 1000
- 0 \leq g < j \leq 1000
- 0 \leq h < k \leq 1000
- 0 \leq i < l \leq 1000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
a b c d e f g h i j k l
Output
Print Yes
if the intersection of the two cuboids has a positive volume, and No
otherwise.
Sample Input 1
0 0 0 4 5 6 2 3 4 5 6 7
Sample Output 1
Yes
The positional relationship of the two cuboids is shown in the figure below, and their intersection has a volume of 8.
Sample Input 2
0 0 0 2 2 2 0 0 2 2 2 4
Sample Output 2
No
The two cuboids touch at a face, where the volume of the intersection is 0.
Sample Input 3
0 0 0 1000 1000 1000 10 10 10 100 100 100
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋くんは回転寿司で N 皿の料理を食べました。 i 皿目の色は文字列 C_i で表されます。
また、料理の価格は皿の色と対応し、 i=1,\ldots,M のそれぞれについて、色が文字列 D_i の皿の料理は一皿 P_i 円です。また、D_1,\ldots,D_M のいずれとも異なる色の皿の料理は一皿 P_0 円です。
高橋くんが食べた料理の価格の合計を求めてください。
制約
- 1\leq N,M\leq 100
- C_i,D_i は英小文字からなる長さ 1 以上 20 以下の文字列
- D_1,\ldots,D_M はすべて異なる
- 1\leq P_i\leq 10000
- N,M,P_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N M C_1 \ldots C_N D_1 \ldots D_M P_0 P_1 \ldots P_M
出力
答えを整数として出力せよ。
入力例 1
3 2 red green blue blue red 800 1600 2800
出力例 1
5200
blue
の皿は P_1 = 1600 円、red
の皿は P_2 = 2800 円、green
の皿は P_0 = 800 円です。
高橋くんが食べた料理の価格の合計は、2800+800+1600=5200 円です。
入力例 2
3 2 code queen atcoder king queen 10 1 1
出力例 2
21
Score : 200 points
Problem Statement
Takahashi ate N plates of sushi at a sushi restaurant. The color of the i-th plate is represented by a string C_i.
The price of a sushi corresponds to the color of the plate. For each i=1,\ldots,M, the sushi on a plate whose color is represented by a string D_i is worth P_i yen a plate (yen is the currency of Japan). If the color does not coincide with any of D_1,\ldots, and D_M, it is worth P_0 yen a plate.
Find the total amount of the prices of sushi that Takahashi ate.
Constraints
- 1\leq N,M\leq 100
- C_i and D_i are strings of length between 1 and 20, inclusive, consisting of lowercase English letters.
- D_1,\ldots, and D_M are distinct.
- 1\leq P_i\leq 10000
- N, M, and P_i are integers.
Input
The input is given from Standard Input in the following format:
N M C_1 \ldots C_N D_1 \ldots D_M P_0 P_1 \ldots P_M
Output
Print the answer as an integer.
Sample Input 1
3 2 red green blue blue red 800 1600 2800
Sample Output 1
5200
A blue
plate, red
plate, and green
plate are worth P_1 = 1600, P_2 = 2800, and P_0 = 800 yen, respectively.
The total amount of the prices of the sushi that he ate is 2800+800+1600=5200 yen.
Sample Input 2
3 2 code queen atcoder king queen 10 1 1
Sample Output 2
21
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の正整数のみからなる数列 A=(A_1,\dots,A_N) があります。
A を 10^{100} 回連結した数列を数列 B とします。
B の項を前から順に足したとき、和が初めて X を超えるのは何項目まで足したときですか?
すなわち、以下の式を満たす最小の整数 k を求めてください。
\displaystyle{\sum_{i=1}^{k} B_i \gt X}
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq X \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N X
出力
答えを出力せよ。
入力例 1
3 3 5 2 26
出力例 1
8
B=(3,5,2,3,5,2,3,5,2,\dots) です。
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} であり、k が 7 以下のとき条件を満たさないので、8 が答えです。
入力例 2
4 12 34 56 78 1000
出力例 2
23
Score : 300 points
Problem Statement
We have a sequence of N positive integers: A=(A_1,\dots,A_N).
Let B be the concatenation of 10^{100} copies of A.
Consider summing up the terms of B from left to right. When does the sum exceed X for the first time?
In other words, find the minimum integer k such that:
\displaystyle{\sum_{i=1}^{k} B_i \gt X}.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq X \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 \ldots A_N X
Output
Print the answer.
Sample Input 1
3 3 5 2 26
Sample Output 1
8
We have B=(3,5,2,3,5,2,3,5,2,\dots).
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} holds, but the condition is not satisfied when k is 7 or less, so the answer is 8.
Sample Input 2
4 12 34 56 78 1000
Sample Output 2
23
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 N 行、横 N 列のマス目があり、上から i 行目、左から j 列目のマスには整数 N\times (i-1)+j が書かれています。
今から T ターンにわたって相異なる整数が宣言されます。i ターン目には A_i が宣言され、A_i が書かれたマスに印をつけます。初めてビンゴを達成するのは何ターン目か求めてください。ただし、T ターンの中でビンゴを達成しない場合は -1
を出力してください。
ここで、ビンゴを達成するとは以下のいずれかのうち少なくとも一つ満たされることを言います。
- マス目の横の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
- マス目の縦の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
- マス目の対角線の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
制約
- 2\leq N\leq 2\times 10^3
- 1\leq T\leq \min(N^2,2\times 10^5)
- 1\leq A_i\leq N^2
- i\neq j ならば A_i\neq A_j
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T A_1 A_2 \ldots A_T
出力
T ターンの中でビンゴを達成するならば初めてビンゴを達成するターンを、そうでないならば -1
を出力せよ。
入力例 1
3 5 5 1 8 9 7
出力例 1
4
マス目の状態は以下のように変化します。初めてビンゴを達成するのは 4 ターン目です。
入力例 2
3 5 4 2 9 7 5
出力例 2
-1
5 ターンの中でビンゴを達成できないので -1
を出力してください。
入力例 3
4 12 13 9 6 5 2 7 16 14 8 3 10 11
出力例 3
9
Score : 300 points
Problem Statement
There is an N \times N grid, where the cell at the i-th row from the top and the j-th column from the left contains the integer N \times (i-1) + j.
Over T turns, integers will be announced. On Turn i, the integer A_i is announced, and the cell containing A_i is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T turns, print -1
.
Here, achieving Bingo means satisfying at least one of the following conditions:
- There exists a row in which all N cells are marked.
- There exists a column in which all N cells are marked.
- There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N cells are marked.
Constraints
- 2 \leq N \leq 2 \times 10^3
- 1 \leq T \leq \min(N^2, 2 \times 10^5)
- 1 \leq A_i \leq N^2
- A_i \neq A_j if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T A_1 A_2 \ldots A_T
Output
If Bingo is achieved within T turns, print the turn number on which Bingo is achieved for the first time; otherwise, print -1
.
Sample Input 1
3 5 5 1 8 9 7
Sample Output 1
4
The state of the grid changes as follows. Bingo is achieved for the first time on Turn 4.
Sample Input 2
3 5 4 2 9 7 5
Sample Output 2
-1
Bingo is not achieved within five turns, so print -1
.
Sample Input 3
4 12 13 9 6 5 2 7 16 14 8 3 10 11
Sample Output 3
9
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君が住んでいる二次元平面上の街には N 個のジャンプ台があります。i 番目のジャンプ台は点 (x_i, y_i) にあり、ジャンプ台のパワーは P_i です。また高橋君のジャンプ力は S で表され、はじめ S=0 です。高橋君が訓練を 1 回行う度に S は 1 増えます。
高橋君は以下の条件を満たす場合に限り、i 番目のジャンプ台から j 番目のジャンプ台にジャンプすることができます。
- P_iS\geq |x_i - x_j| +|y_i - y_j|
高橋君の目的は、適切に始点とするジャンプ台を決めることで、そのジャンプ台からどのジャンプ台にも何回かのジャンプで移動できるようにすることです。
目的を達成するためには高橋君は最低で何回訓練を行う必要があるでしょうか?
制約
- 2 \leq N \leq 200
- -10^9 \leq x_i,y_i \leq 10^9
- 1 \leq P_i \leq 10^9
- (x_i, y_i) \neq (x_j,y_j)\ (i\neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 P_1 \vdots x_N y_N P_N
出力
答えを出力せよ。
入力例 1
4 -10 0 1 0 0 5 10 0 1 11 0 1
出力例 1
2
高橋君が 2 回訓練したとすると、 S=2 です。 このとき、2 番目のジャンプ台から全てのジャンプ台に移動することができます。
例えば、4 番目のジャンプ台へは以下の方法で移動ができます。
-
2 番目のジャンプ台から 3 番目のジャンプ台へジャンプする。( P_2 S = 10, |x_2-x_3| + |y_2-y_3| = 10 であり、 P_2 S \geq |x_2-x_3| + |y_2-y_3| を満たす。)
-
3 番目のジャンプ台から 4 番目のジャンプ台へジャンプする。( P_3 S = 2, |x_3-x_4| + |y_3-y_4| = 1 であり、 P_3 S \geq |x_3-x_4| + |y_3-y_4| を満たす。)
入力例 2
7 20 31 1 13 4 3 -10 -15 2 34 26 5 -2 39 4 0 -50 1 5 -20 2
出力例 2
18
Score : 400 points
Problem Statement
There are N trampolines on a two-dimensional planar town where Takahashi lives. The i-th trampoline is located at the point (x_i, y_i) and has a power of P_i. Takahashi's jumping ability is denoted by S. Initially, S=0. Every time Takahashi trains, S increases by 1.
Takahashi can jump from the i-th to the j-th trampoline if and only if:
- P_iS\geq |x_i - x_j| +|y_i - y_j|.
Takahashi's objective is to become able to choose a starting trampoline such that he can reach any trampoline from the chosen one with some jumps.
At least how many times does he need to train to achieve his objective?
Constraints
- 2 \leq N \leq 200
- -10^9 \leq x_i,y_i \leq 10^9
- 1 \leq P_i \leq 10^9
- (x_i, y_i) \neq (x_j,y_j)\ (i\neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N x_1 y_1 P_1 \vdots x_N y_N P_N
Output
Print the answer.
Sample Input 1
4 -10 0 1 0 0 5 10 0 1 11 0 1
Sample Output 1
2
If he trains twice, S=2, in which case he can reach any trampoline from the 2-nd one.
For example, he can reach the 4-th trampoline as follows.
-
Jump from the 2-nd to the 3-rd trampoline. (Since P_2 S = 10 and |x_2-x_3| + |y_2-y_3| = 10, it holds that P_2 S \geq |x_2-x_3| + |y_2-y_3|.)
-
Jump from the 3-rd to the 4-th trampoline. (Since P_3 S = 2 and |x_3-x_4| + |y_3-y_4| = 1, it holds that P_3 S \geq |x_3-x_4| + |y_3-y_4|.)
Sample Input 2
7 20 31 1 13 4 3 -10 -15 2 34 26 5 -2 39 4 0 -50 1 5 -20 2
Sample Output 2
18
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
H \times W の大きさの島があり、島は周りを海で囲まれています。
島は 縦 H 個 \times 横 W 個の 1\times 1 の区画に分けられており、上から i 番目かつ左から j 番目の区画の(現在の海面を基準にした)標高は A_{i,j} です。
現在から 1 年ごとに海面の高さが 1 ずつ上昇します。
このとき、海または海に沈んだ区画に上下左右に隣接する区画であって、標高が海面の高さ 以下 の区画は海に沈みます。
ここで、ある区画が新しく海に沈んだときそれと上下左右に隣接する区画であって海面の高さ以下のものも同時に海に沈み、これによって新しく沈んだ区画についてもこれは繰り返されます。
i=1,2,\ldots, Y それぞれについて、現在から i 年後に、島のうち海に沈まず残っている部分の面積を求めてください。
制約
- 1\leq H,W\leq 1000
- 1\leq Y\leq 10^5
- 1\leq A_{i,j}\leq 10^5
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W Y A_{1,1} A_{1,2} \ldots A_{1,W} A_{2,1} A_{2,2} \ldots A_{2,W} \vdots A_{H,1} A_{H,2} \ldots A_{H,W}
出力
Y 行出力せよ。 i 行目 (1\leq i\leq Y) には現在から i 年後に海に沈まず残っている島の面積を出力せよ。
入力例 1
3 3 5 10 2 10 3 1 4 10 5 10
出力例 1
9 7 6 5 4
島の上から i 番目かつ左から j 番目の区画を (i,j) で表します。このとき、次のようになります。
- 1 年後、海面は現在より 1 上昇しますが、海に面している標高 1 の区画は存在しないため、どの区画も沈みません。よって、1 行目には 9 を出力します。
- 2 年後、海面は現在より 2 上昇し、(1,2) が海に沈みます。これによって、(2,2) は海に沈んだ区画に隣接する区画となりますが、その標高は 2 以下であるため、これも海に沈みます。これら以外にこの時点で他に沈む区画はありません。よって、2 つの区画が沈むため、2 行目には 9-2=7 を出力します。
- 3 年後、海面は現在より 3 上昇し、(2,1) が新しく海に沈みます。他に沈む区画はありません。よって、3 行目には 6 を出力します。
- 4 年後、海面は現在より 4 上昇し、(2,3) が新しく海に沈みます。他に沈む区画はありません。よって、4 行目には 5 を出力します。
- 5 年後、海面は現在より 5 上昇し、(3,2) が新しく海に沈みます。他に沈む区画はありません。よって、5 行目には 4 を出力します。
よって、9,7,6,5,4 をこの順に各行に出力します。
入力例 2
3 5 3 2 2 3 3 3 2 1 2 1 3 2 2 3 3 3
出力例 2
15 7 0
Score : 450 points
Problem Statement
There is an island of size H \times W, surrounded by the sea.
The island is divided into H rows and W columns of 1 \times 1 sections, and the elevation of the section at the i-th row from the top and the j-th column from the left (relative to the current sea level) is A_{i,j}.
Starting from now, the sea level rises by 1 each year.
Here, a section that is vertically or horizontally adjacent to the sea or a section sunk into the sea and has an elevation not greater than the sea level will sink into the sea.
Here, when a section newly sinks into the sea, any vertically or horizontally adjacent section with an elevation not greater than the sea level will also sink into the sea simultaneously, and this process repeats for the newly sunk sections.
For each i=1,2,\ldots, Y, find the area of the island that remains above sea level i years from now.
Constraints
- 1 \leq H, W \leq 1000
- 1 \leq Y \leq 10^5
- 1 \leq A_{i,j} \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W Y A_{1,1} A_{1,2} \ldots A_{1,W} A_{2,1} A_{2,2} \ldots A_{2,W} \vdots A_{H,1} A_{H,2} \ldots A_{H,W}
Output
Print Y lines. The i-th line (1 \leq i \leq Y) should contain the area of the island that remains above sea level i years from now.
Sample Input 1
3 3 5 10 2 10 3 1 4 10 5 10
Sample Output 1
9 7 6 5 4
Let (i,j) denote the section at the i-th row from the top and the j-th column from the left. Then, the following happens:
- After 1 year, the sea level is higher than now by 1, but there are no sections with an elevation of 1 that are adjacent to the sea, so no sections sink. Thus, the first line should contain 9.
- After 2 years, the sea level is higher than now by 2, and (1,2) sinks into the sea. This makes (2,2) adjacent to a sunken section, and its elevation is not greater than 2, so it also sinks. No other sections sink at this point. Thus, two sections sink, and the second line should contain 9-2=7.
- After 3 years, the sea level is higher than now by 3, and (2,1) sinks into the sea. No other sections sink. Thus, the third line should contain 6.
- After 4 years, the sea level is higher than now by 4, and (2,3) sinks into the sea. No other sections sink. Thus, the fourth line should contain 5.
- After 5 years, the sea level is higher than now by 5, and (3,2) sinks into the sea. No other sections sink. Thus, the fifth line should contain 4.
Therefore, print 9, 7, 6, 5, 4 in this order, each on a new line.
Sample Input 2
3 5 3 2 2 3 3 3 2 1 2 1 3 2 2 3 3 3
Sample Output 2
15 7 0
Time Limit: 2 sec / Memory Limit: 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
.