実行時間制限: 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
配点 : 100 点
問題文
xy 平面上に長方形があります。この長方形の各辺は x 軸または y 軸に平行であり、面積は 0 ではありません。
この長方形の 4 つの頂点のうち異なる 3 つの頂点の座標 (x_1, y_1), (x_2, y_2), (x_3, y_3) が与えられるので、残る 1 つの頂点の座標を求めてください。
制約
- -100 \leq x_i, y_i \leq 100
- (x_1, y_1), (x_2, y_2), (x_3, y_3) のすべてを頂点に持つ長方形がただ一つ存在し、その各辺は x 軸または y 軸に平行であり、面積は 0 ではない。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
x_1 y_1 x_2 y_2 x_3 y_3
出力
答えとなる頂点の座標 (x, y) を下記の形式にしたがい空白区切りで出力せよ。
x y
入力例 1
-1 -1 -1 2 3 2
出力例 1
3 -1
(-1, -1), (-1, 2), (3, 2) を頂点とする長方形の残る 1 つの頂点は (3, -1) です。
入力例 2
-60 -40 -60 -80 -20 -80
出力例 2
-20 -40
Score : 100 points
Problem Statement
There is a rectangle in the xy-plane. Each edge of this rectangle is parallel to the x- or y-axis, and its area is not zero.
Given the coordinates of three of the four vertices of this rectangle, (x_1, y_1), (x_2, y_2), and (x_3, y_3), find the coordinates of the other vertex.
Constraints
- -100 \leq x_i, y_i \leq 100
- There uniquely exists a rectangle with all of (x_1, y_1), (x_2, y_2), (x_3, y_3) as vertices, edges parallel to the x- or y-axis, and a non-zero area.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
x_1 y_1 x_2 y_2 x_3 y_3
Output
Print the sought coordinates (x, y) separated by a space in the following format:
x y
Sample Input 1
-1 -1 -1 2 3 2
Sample Output 1
3 -1
The other vertex of the rectangle with vertices (-1, -1), (-1, 2), (3, 2) is (3, -1).
Sample Input 2
-60 -40 -60 -80 -20 -80
Sample Output 2
-20 -40
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
長さ N の正整数列 A = (A_1, A_2, \dots ,A_N) が与えられます。高橋くんは、以下の操作を A に含まれる正の要素の個数が 1 つ以下になるまで繰り返します。
- A を要素の降順に並び替える。それから、 A_1, A_2 を 1 減らす。
高橋くんが操作をする回数を求めてください。
制約
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \cdots A_N
出力
答えを出力せよ。
入力例 1
4 1 2 3 3
出力例 1
4
操作は以下のように進みます。
- 1 回目の操作で A = (2, 2, 2, 1) となる。
- 2 回目の操作で A = (1, 1, 2, 1) となる。
- 3 回目の操作で A = (1, 0, 1, 1) となる。
- 4 回目の操作で A = (0, 0, 1, 0) となる。A に含まれる正の要素の個数が 1 つ以下になったので、ここで操作を終了する。
入力例 2
3 1 1 100
出力例 2
2
Score : 200 points
Problem Statement
You are given a sequence of N positive integers A = (A_1, A_2, \dots ,A_N). Takahashi repeats the following operation until A contains one or fewer positive elements:
- Sort A in descending order. Then, decrease both A_1 and A_2 by 1.
Find the number of times he performs this operation.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \cdots A_N
Output
Print the answer.
Sample Input 1
4 1 2 3 3
Sample Output 1
4
The process goes as follows:
- After the 1st operation, A is (2, 2, 2, 1).
- After the 2nd operation, A is (1, 1, 2, 1).
- After the 3rd operation, A is (1, 0, 1, 1).
- After the 4th operation, A is (0, 0, 1, 0). A no longer contains more than one positive elements, so the process ends here.
Sample Input 2
3 1 1 100
Sample Output 2
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
キーエンスでは良いことも悪いこともありのままに報告するという文化があります。
そこで、報告内容が元の文章のありのままであるかを確認したいです。
英小文字のみからなる文字列 S, T が与えられます。
S と T が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力してください。
ただし、S,T の一方にのみ i 文字目が存在するときも、i 文字目は異なっているとみなすものとします。
より厳密には、S と T が等しくないならば次の条件のうちいずれかをみたす最小の整数 i を出力してください。
- 1\leq i\leq |S| かつ 1\leq i\leq |T| かつ S_i\neq T_i
- |S|< i\leq |T|
- |T|< i\leq |S|
ただし、|S|,|T| でそれぞれ S,T の長さを、S_i,T_i でそれぞれ S,T の i 文字目を表します。
制約
- S,T は英小文字のみからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S と T が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力せよ。
入力例 1
abcde abedc
出力例 1
3
S=abcde, T=abedc です。
S と T は 1,2 文字目は等しく、3 文字目が異なるため、 3 を出力します。
入力例 2
abcde abcdefg
出力例 2
6
S=abcde, T=abcdefg です。
S と T は 5 文字目まで等しく、T にのみ 6 文字目が存在するため、6 を出力します。
入力例 3
keyence keyence
出力例 3
0
S と T は等しいため、 0 を出力します。
Score : 200 points
Problem Statement
KEYENCE has a culture of reporting things as they are, whether good or bad.
So we want to check whether the reported content is exactly the same as the original text.
You are given two strings S and T, consisting of lowercase English letters.
If S and T are equal, print 0; otherwise, print the position of the first character where they differ.
Here, if the i-th character exists in only one of S and T, consider that the i-th characters are different.
More precisely, if S and T are not equal, print the smallest integer i satisfying one of the following conditions:
- 1\leq i\leq |S|, 1\leq i\leq |T|, and S_i\neq T_i.
- |S| < i \leq |T|.
- |T| < i \leq |S|.
Here, |S| and |T| denote the lengths of S and T, respectively, and S_i and T_i denote the i-th characters of S and T, respectively.
Constraints
- S and T are strings 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 T
Output
If S and T are equal, print 0; otherwise, print the position of the first character where they differ.
Sample Input 1
abcde abedc
Sample Output 1
3
We have S= abcde and T= abedc.
S and T have the same first and second characters, but differ at the third character, so print 3.
Sample Input 2
abcde abcdefg
Sample Output 2
6
We have S= abcde and T= abcdefg.
S and T are equal up to the fifth character, but only T has a sixth character, so print 6.
Sample Input 3
keyence keyence
Sample Output 3
0
S and T are equal, so print 0.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。
制約
- 入力は全て整数
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 1 5 4 9
出力例 1
2
A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。
- 例えば 2 要素目の 1 、 5 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
- このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。
入力例 2
6 5 1 1 1 1 1 1
出力例 2
0
入力例 3
8 3 31 43 26 6 18 36 22 13
出力例 3
18
Score : 250 points
Problem Statement
You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.
Constraints
- All inputs are integers.
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 1 5 4 9
Sample Output 1
2
Consider removing exactly two elements from A=(3,1,5,4,9).
- For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
- In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.
Sample Input 2
6 5 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
8 3 31 43 26 6 18 36 22 13
Sample Output 3
18
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
H 行 W 列のマス目の上に 0 個以上のセンサが配置されています。上から i 行目、左から j 列目のマス目を (i, j) と表記します。
センサが配置されているマス目の情報は長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_i の j 文字目が # のとき、またそのときに限り (i, j) にセンサが配置されています。
このセンサは上下左右斜めに隣接しているマス目に存在する他のセンサと連動し、一つのセンサとして動作します。
ただし、マス目 (x, y) と (x', y') が上下左右斜めに隣接しているとは、\max(|x-x'|,|y-y'|) = 1 であることを指します。
また、センサ A とセンサ B が連動し、センサ A とセンサ C が連動しているとき、センサ B とセンサ C も連動することに注意してください。
連動するセンサを一つのセンサと見なしたとき、このマス目の上にあるセンサの個数を求めてください。
制約
- 1 \leq H, W \leq 1000
- H, W は整数
- S_i は各文字が
#または.である長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
5 6 .##... ...#.. ....## #.#... ..#...
出力例 1
3
連動しているセンサを一つのセンサと見なしたとき、
- (1,2),(1,3),(2,4),(3,5),(3,6) にあるセンサが連動したもの
- (4,1) にあるセンサ
- (4,3),(5,3) にあるセンサが連動したもの
の 3 つのセンサが存在します。
入力例 2
3 3 #.# .#. #.#
出力例 2
1
入力例 3
4 2 .. .. .. ..
出力例 3
0
入力例 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
出力例 4
7
Score : 300 points
Problem Statement
There are zero or more sensors placed on a grid of H rows and W columns. Let (i, j) denote the square in the i-th row from the top and the j-th column from the left.
Whether each square contains a sensor is given by the strings S_1, S_2, \ldots, S_H, each of length W. (i, j) contains a sensor if and only if the j-th character of S_i is #.
These sensors interact with other sensors in the squares horizontally, vertically, or diagonally adjacent to them and operate as one sensor.
Here, a cell (x, y) and a cell (x', y') are said to be horizontally, vertically, or diagonally adjacent if and only if \max(|x-x'|,|y-y'|) = 1.
Note that if sensor A interacts with sensor B and sensor A interacts with sensor C, then sensor B and sensor C also interact.
Considering the interacting sensors as one sensor, find the number of sensors on this grid.
Constraints
- 1 \leq H, W \leq 1000
- H and W are integers.
- S_i is a string of length W where each character is
#or..
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print the answer.
Sample Input 1
5 6 .##... ...#.. ....## #.#... ..#...
Sample Output 1
3
When considering the interacting sensors as one sensor, the following three sensors exist:
- The interacting sensors at (1,2),(1,3),(2,4),(3,5),(3,6)
- The sensor at (4,1)
- The interacting sensors at (4,3),(5,3)
Sample Input 2
3 3 #.# .#. #.#
Sample Output 2
1
Sample Input 3
4 2 .. .. .. ..
Sample Output 3
0
Sample Input 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
Sample Output 4
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
両面に整数が書かれたカードが N 枚あり、i \, (1 \leq i \leq N) 枚目のカードの表には a_i が、裏には b_i が書かれています。
あなたは、それぞれのカードについて、表を上に向けて置くか裏を上に向けて置くかを自由に決めることができます。
上に向けられた面に書かれた整数の総和がちょうど S となるようにカードを置くことができるか判定し、可能ならそのようなカードの置き方の一例を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq S \leq 10000
- 1 \leq a_i, b_i \leq 100 \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N S a_1 b_1 \vdots a_N b_N
出力
まず、上に向けられた面に書かれた整数の総和がちょうど S となるようにカードを置くことができるならば Yes を、できなければ No を出力し、改行せよ。
さらに、そのようにカードを置くことができるならば、カードの置き方を表す H および T のみからなる長さ N の文字列を出力せよ。
この文字列の i \, (1 \leq i \leq N) 文字目は、i 枚目のカードを表を上に向けて置くなら H、裏を上に向けて置くなら T でなければならない。
条件を満たすカードの置き方が複数考えられる場合、どれを出力しても正解となる。
入力例 1
3 11 1 4 2 3 5 7
出力例 1
Yes THH
例えば次のように置くことで、上に向けられた面に書かれた整数の総和がちょうど S (= 11) となります。
- 1 枚目は表、2 枚目は裏、3 枚目は裏を上に向けて置く。
- 1 枚目は裏、2 枚目は表、3 枚目は表を上に向けて置く。
よって、HTT や THH といった出力が正解となります。
入力例 2
5 25 2 8 9 3 4 11 5 1 12 6
出力例 2
No
上に向けられた面に書かれた整数の総和がちょうど S (= 25) となるようにカードを置くことはできません。
Score : 400 points
Problem Statement
There are N cards with an integer written on each side. Card i (1 \leq i \leq N) has an integer a_i written on the front and an integer b_i written on the back.
You may choose whether to place each card with its front or back side visible.
Determine if you can place the cards so that the sum of the visible integers exactly equals S. If possible, find a placement of the cards to realize it.
Constraints
- 1 \leq N \leq 100
- 1 \leq S \leq 10000
- 1 \leq a_i, b_i \leq 100 \, (1 \leq i \leq N)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N S a_1 b_1 \vdots a_N b_N
Output
First, print Yes if you can make the sum of the visible integers exactly equal S, and No otherwise, followed by a newline.
Additionally, if such a placement is possible, print a string of length N consisting of H and T that represents the placement of the cards to realize it.
The i-th (1 \leq i \leq N) character should be H if the i-th card should be placed with its front side visible, and T with its back.
If there are multiple possible placements to realize the sum, printing any of them is accepted.
Sample Input 1
3 11 1 4 2 3 5 7
Sample Output 1
Yes THH
For example, the following placements make the sum of the visible integers exactly equal S (= 11):
- Place the 1-st card with its front side visible, 2-nd with its back, and 3-rd with its back.
- Place the 1-st card with its back side visible, 2-nd with its front, and 3-rd with its front.
Therefore, outputs like HTT and THH are accepted.
Sample Input 2
5 25 2 8 9 3 4 11 5 1 12 6
Sample Output 2
No
You cannot place the cards so that the sum of the visible integers exactly equals S (= 25).
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の都市があり、いくつかの相異なる都市の間は一方通行の直行便によって移動することができます。
どの直行便が存在するかは N 個の長さ N の文字列 S_1,S_2,\ldots,S_N によって表され、
S_i の j 文字目が Y のとき都市 i から都市 j へ向かう直行便が存在することを、
N のとき存在しないことを示します。
また、各都市ではお土産が 1 つずつ売られており、都市 i では価値 A_i のお土産が売られています。
次のような問題を考えます。
高橋君は今都市 S におり、いくつかの直行便を乗り継いで(S とは異なる)都市 T に向かおうとしています。
さらに、高橋君は移動する途中で訪れた都市(S,T を含む)において 1 つずつお土産を購入します。
都市 S から都市 T へ向かう経路が複数存在する時、高橋君は次のような経路で移動することを考えています。
- 都市 S から 都市 T に向かう経路のうち、使う直行便の数が最小である。
- さらにそのような経路のうち、購入するお土産の価値の総和が最大である。
高橋君が都市 S から T に直行便を乗り継いで移動することが可能か判定し、 可能な時は高橋君が上の条件をみたすように経路を選んだ時の「使用する直行便の本数」と「お土産の価値の総和」を求めよ。
相異なる都市の組 (U_i,V_i) が Q 個与えられます。
各 1\leq i\leq Q について、S=U_i, T=V_i とした時の上記の問題の答えを出力してください。
制約
- 2 \leq N \leq 300
- 1\leq A_i\leq 10^9
- S_i は
YとNのみからなる長さ N の文字列 - S_i の i 文字目は
N - 1\leq Q\leq N(N-1)
- 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,A_i,Q,U_i,V_i は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N S_1 S_2 \vdots S_N Q U_1 V_1 U_2 V_2 \vdots U_Q V_Q
出力
Q 行出力せよ。
i (1\leq i\leq Q) 行目には、
都市 U_i から都市 V_i に移動することが不可能な時は Impossible を、
可能な時は上記のように経路を選んだ時の「使用する直行便の本数」と「お土産の価値の総和」をこの順に空白区切りで出力せよ。
入力例 1
5 30 50 70 20 60 NYYNN NNYNN NNNYY YNNNN YNNNN 3 1 3 3 1 4 5
出力例 1
1 100 2 160 3 180
(S,T)=(U_1,V_1)=(1,3) の時について、都市 1 から都市 3 への直行便が存在するため、 使用する直行便としてあり得る最小の値はその直行便を使用した時の 1 本となります。 この時、お土産の価値の総和は A_1+A_3=30+70=100 となります。
(S,T)=(U_2,V_2)=(3,1) の時について、使用する直行便としてあり得る最小の値は 2 本で、 最小値を達成する経路としては都市 3\to 4\to 1 と都市 3\to 5\to 1 の 2 通りが考えられます。 それぞれの経路の時のお土産の価値の総和は 70+20+30=120, 70+60+30=160 なので、 高橋君は後者の経路を選び、この時お土産の価値の総和は 160 となります。
(S,T)=(U_3,V_3)=(4,5) の時について、都市 4\to 1\to 3\to 5 と進んだ時に使用する直行便の本数は最小となり、 この時お土産の価値の総和は 20+30+70+60=180 となります。
入力例 2
2 100 100 NN NN 1 1 2
出力例 2
Impossible
直行便が全く存在しないこともあります。
Score : 500 points
Problem Statement
There are N cities. There are also one-way direct flights that connect different cities.
The availability of direct flights is represented by N strings S_1,S_2,\ldots,S_N of length N each.
If the j-th character of S_i is Y, there is a direct flight from city i to city j;
if it is N, there is not.
Each city sells a souvenir; city i sells a souvenir of value A_i.
Consider the following problem:
Takahashi is currently at city S and wants to get to city T (that is different from city S) using some direct flights.
Every time he visits a city (including S and T), he buys a souvenir there.
If there are multiple routes from city S to city T, Takahashi decides the route as follows:
- He tries to minimize the number of direct flights in the route from city S to city T.
- Then he tries to maximize the total value of the souvenirs he buys.
Determine if he can travel from city S to city T using the direct flights. If he can, find the "number of direct flights" and "total value of souvenirs" in the route that satisfies the conditions above.
You are given Q pairs (U_i,V_i) of distinct cities.
For each 1\leq i\leq Q, print the answer to the problem above when S=U_i and T=V_i.
Constraints
- 2 \leq N \leq 300
- 1\leq A_i\leq 10^9
- S_i is a string of length N consisting of
YandN. - The i-th character of S_i is
N. - 1\leq Q\leq N(N-1)
- 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).
- N,A_i,Q,U_i, and V_i are all integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N S_1 S_2 \vdots S_N Q U_1 V_1 U_2 V_2 \vdots U_Q V_Q
Output
Print Q lines.
The i-th (1\leq i\leq Q) line should contain
Impossible if he cannot travel from city U_i to city V_i;
if he can, the line should contain "the number of direct flights" and "the total value of souvenirs" in the route chosen as described above, in this order, separated by a space.
Sample Input 1
5 30 50 70 20 60 NYYNN NNYNN NNNYY YNNNN YNNNN 3 1 3 3 1 4 5
Sample Output 1
1 100 2 160 3 180
For (S,T)=(U_1,V_1)=(1,3), there is a direct flight from city 1 to city 3, so the minimum possible number of direct flights is 1, which is achieved when he uses that direct flight. In this case, the total value of the souvenirs is A_1+A_3=30+70=100.
For (S,T)=(U_2,V_2)=(3,1), the minimum possible number of direct flights is 2. The following two routes achieve the minimum: cities 3\to 4\to 1, and cities 3\to 5\to 1. Since the total value of souvenirs in the two routes are 70+20+30=120 and 70+60+30=160, respectively, so he chooses the latter route, and the total value of the souvenirs is 160.
For (S,T)=(U_3,V_3)=(4,5), the number of direct flights is minimum when he travels cities 4\to 1\to 3\to 5, where the total value of the souvenirs is 20+30+70+60=180.
Sample Input 2
2 100 100 NN NN 1 1 2
Sample Output 2
Impossible
There may be no direct flight at all.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
頂点に 1 から N の番号がつけられた N 頂点の木および整数 K が与えられます。i 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。
あなたはいま頂点 1 にいます。以下の操作を 0 回以上繰り返すことができます。
- あなたが今いる頂点からの距離が K であるような頂点を一つ選び、その頂点に移動する。ただし、2 頂点間の距離は 2 頂点を結ぶ単純パスに含まれる辺の個数とする。
各 k=2,\ldots,N に対して、操作を繰り返して頂点 k に移動することができるかどうか判定し、移動できるならば操作回数の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて解いてください。
制約
- 1\leq T\leq 10^5
- 2\leq N\leq 2\times 10^5
- 1\leq K\leq 20
- 1\leq u_i\lt v_i\leq N
- 与えられるグラフは木
- 全てのテストケースに対する N の総和は 2\times 10^5 以下
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N K
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを以下の形式で出力せよ。
\mathrm{ans}_2 \mathrm{ans}_3 \ldots \mathrm{ans}_N
\mathrm{ans}_i は k=i に対する答えである。操作を繰り返して頂点 i に移動することができるならば操作回数の最小値を、移動することができないならば -1 とせよ。
入力例 1
1 6 2 1 2 2 3 2 4 4 5 5 6
出力例 1
-1 1 1 -1 2
この入出力例は 1 つのテストケースからなります。
頂点 1 との距離が 2 である頂点は 3,4 であるため、この 2 つの頂点には 1 回の操作で移動することができます。
また、頂点 1 から頂点 4 に移動したのち、頂点 4 からの距離が 2 である頂点 6 に移動することで、頂点 6 には 2 回の操作で移動することができます。
頂点 2,5 にはどのように操作しても移動することができません。
入力例 2
3 2 20 1 2 10 2 1 9 1 8 1 5 6 8 4 5 2 8 5 10 7 9 3 5 10 1 2 6 2 9 8 9 9 10 3 9 4 9 7 9 1 6 3 5
出力例 2
-1 1 1 1 -1 1 1 -1 -1 1 2 4 4 5 1 4 4 3 4
Score : 525 points
Problem Statement
You are given a tree with N vertices numbered from 1 to N and an integer K. The i-th edge bidirectionally connects vertices u_i and v_i.
You are currently at vertex 1. You can repeat the following operation zero or more times:
- Choose a vertex whose distance from the vertex you are currently at is K, and move to that vertex. Here, the distance between two vertices is the number of edges in the simple path connecting the two vertices.
For each k=2,\ldots,N, determine whether you can move to vertex k by repeating the operation, and if you can move, find the minimum number of operations.
You have T test cases, so solve each of them.
Constraints
- 1\leq T\leq 10^5
- 2\leq N\leq 2\times 10^5
- 1\leq K\leq 20
- 1\leq u_i\lt v_i\leq N
- The given graph is a tree.
- The sum of N over all test cases is at most 2\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{case}_i means the i-th test case.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N K
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Output T lines. The i-th line should contain the answer for the i-th test case in the following format:
\mathrm{ans}_2 \mathrm{ans}_3 \ldots \mathrm{ans}_N
\mathrm{ans}_i is the answer for k=i. If you can move to vertex i by repeating the operation, \mathrm{ans}_i is the minimum number of operations; otherwise, \mathrm{ans}_i is -1.
Sample Input 1
1 6 2 1 2 2 3 2 4 4 5 5 6
Sample Output 1
-1 1 1 -1 2
This sample input/output consists of one test case.
The vertices whose distance from vertex 1 is 2 are vertices 3 and 4, so you can move to these two vertices with one operation.
Also, by moving from vertex 1 to vertex 4, then moving to vertex 6 whose distance from vertex 4 is 2, you can move to vertex 6 with two operations.
You cannot move to vertices 2 and 5 no matter how you perform the operations.
Sample Input 2
3 2 20 1 2 10 2 1 9 1 8 1 5 6 8 4 5 2 8 5 10 7 9 3 5 10 1 2 6 2 9 8 9 9 10 3 9 4 9 7 9 1 6 3 5
Sample Output 2
-1 1 1 1 -1 1 1 -1 -1 1 2 4 4 5 1 4 4 3 4