A - Divide a Cuboid

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

1×1×1 のブロックが A×B×C の直方体状に並んでいます。 高橋君は各ブロックを赤色または青色で塗ろうとしています。 このとき、次の条件が成り立つようにします。

  • 赤いブロックも青いブロックもそれぞれ 1 個以上ある。
  • 赤いブロック全体が 1 つの直方体状になっている。
  • 青いブロック全体が 1 つの直方体状になっている。

高橋君は、赤いブロックの個数と青いブロックの個数の差をできるだけ小さくしたいと思っています。 赤いブロックの個数と青いブロックの個数の差の最小値を求めてください。

制約

  • 2≤A,B,C≤10^9

入力

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

A B C

出力

赤いブロックの個数と青いブロックの個数の差の最小値を出力せよ。


入力例 1

3 3 3

出力例 1

9

例えば、図のように塗ればよいです。 赤いブロックは 9 個で、青いブロックは 18 個なので、個数の差は 9 です。


入力例 2

2 2 4

出力例 2

0

例えば、図のように塗ればよいです。 赤いブロックは 8 個で、青いブロックも 8 個なので、個数の差は 0 です。


入力例 3

5 3 5

出力例 3

15

例えば、図のように塗ればよいです。 赤いブロックは 45 個で、青いブロックは 30 個なので、個数の差は 15 です。

Score : 200 points

Problem Statement

We have a rectangular parallelepiped of size A×B×C, built with blocks of size 1×1×1. Snuke will paint each of the A×B×C blocks either red or blue, so that:

  • There is at least one red block and at least one blue block.
  • The union of all red blocks forms a rectangular parallelepiped.
  • The union of all blue blocks forms a rectangular parallelepiped.

Snuke wants to minimize the difference between the number of red blocks and the number of blue blocks. Find the minimum possible difference.

Constraints

  • 2≤A,B,C≤10^9

Input

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

A B C

Output

Print the minimum possible difference between the number of red blocks and the number of blue blocks.


Sample Input 1

3 3 3

Sample Output 1

9

For example, Snuke can paint the blocks as shown in the diagram below. There are 9 red blocks and 18 blue blocks, thus the difference is 9.


Sample Input 2

2 2 4

Sample Output 2

0

For example, Snuke can paint the blocks as shown in the diagram below. There are 8 red blocks and 8 blue blocks, thus the difference is 0.


Sample Input 3

5 3 5

Sample Output 3

15

For example, Snuke can paint the blocks as shown in the diagram below. There are 45 red blocks and 30 blue blocks, thus the difference is 9.

B - Colorful Slimes

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

高橋君は異世界に住んでいます。 この世界には N 色のスライムがいます。 最初、高橋君はどの色のスライムも飼っていません。 高橋君の目標は、全色のスライムを一緒に飼うことです。

高橋君は次の 2 種類の操作を行えます。

  • 今飼っていないスライムの色 i (1≤i≤N) をひとつ選ぶ。 色 i のスライムを捕まえて飼う。 この操作には a_i 秒掛かる。
  • 魔法を唱える。 すると、今飼っている各スライムについて、色 i (1≤i≤N-1) のスライムは色 i+1 へ変色する。 ただし、色 N のスライムは色 1 へ変色する。 この操作には x 秒掛かる。

高橋君が全色のスライムを一緒に飼うためには、最短で合計何秒掛かるかを求めてください。

制約

  • 2≤N≤2,000
  • a_i は整数である。
  • 1≤a_i≤10^9
  • x は整数である。
  • 1≤x≤10^9

入力

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

N x
a_1 a_2 ... a_N

出力

高橋君が全色のスライムを一緒に飼うためには、最短で合計何秒掛かるかを出力せよ。


入力例 1

2 10
1 100

出力例 1

12

次のように操作を行えばよいです。

  • 1 のスライムを捕まえて飼う。 1 秒掛かる。
  • 魔法を唱える。 スライムの色が 12 と変わる。 10 秒掛かる。
  • 1 のスライムを捕まえて飼う。 1 秒掛かる。

入力例 2

3 10
100 1 100

出力例 2

23

次のように操作を行えばよいです。

  • 2 のスライムを捕まえて飼う。 1 秒掛かる。
  • 魔法を唱える。 スライムの色が 23 と変わる。 10 秒掛かる。
  • 2 のスライムを捕まえて飼う。 1 秒掛かる。
  • 魔法を唱える。 スライムの色が 3123 とそれぞれ変わる。 10 秒掛かる。
  • 2 のスライムを捕まえて飼う。 1 秒掛かる。

入力例 3

4 10
1 2 3 4

出力例 3

10

次のように操作を行えばよいです。

  • 1 のスライムを捕まえて飼う。 1 秒掛かる。
  • 2 のスライムを捕まえて飼う。 2 秒掛かる。
  • 3 のスライムを捕まえて飼う。 3 秒掛かる。
  • 4 のスライムを捕まえて飼う。 4 秒掛かる。

Score : 400 points

Problem Statement

Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.

Snuke can perform the following two actions:

  • Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.

  • Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.

Find the minimum time that Snuke needs to have slimes in all N colors.

Constraints

  • 2≤N≤2,000
  • a_i are integers.
  • 1≤a_i≤10^9
  • x is an integer.
  • 1≤x≤10^9

Input

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

N x
a_1 a_2 ... a_N

Output

Find the minimum time that Snuke needs to have slimes in all N colors.


Sample Input 1

2 10
1 100

Sample Output 1

12

Snuke can act as follows:

  • Catch a slime in color 1. This takes 1 second.
  • Cast the spell. The color of the slime changes: 12. This takes 10 seconds.
  • Catch a slime in color 1. This takes 1 second.

Sample Input 2

3 10
100 1 100

Sample Output 2

23

Snuke can act as follows:

  • Catch a slime in color 2. This takes 1 second.
  • Cast the spell. The color of the slime changes: 23. This takes 10 seconds.
  • Catch a slime in color 2. This takes 1 second.
  • Cast the soell. The color of each slime changes: 31, 23. This takes 10 seconds.
  • Catch a slime in color 2. This takes 1 second.

Sample Input 3

4 10
1 2 3 4

Sample Output 3

10

Snuke can act as follows:

  • Catch a slime in color 1. This takes 1 second.
  • Catch a slime in color 2. This takes 2 seconds.
  • Catch a slime in color 3. This takes 3 seconds.
  • Catch a slime in color 4. This takes 4 seconds.
C - AND Grid

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 700

問題文

高橋君と青木君は、縦 H マス、横 W マスの透明な方眼紙を 1 枚ずつ手に入れました。

高橋君は、自分の方眼紙のいくつかのマスを赤く塗りました。 このとき、赤いマスは上下左右に連結でした。 つまり、どの赤いマスからどの赤いマスへも、上下左右に隣り合う赤いマスのみを辿って行き来できます。

また、青木君は、自分の方眼紙のいくつかのマスを青く塗りました。 このとき、青いマスは上下左右に連結でした。

その後、高橋君と青木君は、2 枚の方眼紙をそのままの向きでぴったりと重ねました。 すると、赤いマスと青いマスが重なるマスのみが紫色になって見えました。

紫色のマスの配置が、長方形に並ぶ文字 a_{ij} (1≤i≤H1≤j≤W) として与えられます。 上から i 行目、左から j 列目のマスが紫色ならば、a_{ij}# であり、紫色でなければ、a_{ij}. です。 このとき、最も外側のマスは紫色でないことが保証されます。 つまり、i=1,H または j=1,W ならば、a_{ij}. です。

問題文の条件を満たすような、赤いマスの配置と青いマスの配置のペアをひとつ求めてください。 解は必ず存在することが示せます。

制約

  • 3≤H,W≤500
  • a_{ij}# または . である。
  • i=1,H または j=1,W ならば、a_{ij}. である。
  • a_{ij} のうち少なくとも 1 つは # である。

入力

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

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

出力

問題文の条件を満たすような、赤いマスの配置と青いマスの配置のペアをひとつ出力せよ。

  • 1 行目から H 行目までには、赤いマスの配置を出力せよ。
  • H+1 行目には、空行を出力せよ。
  • H+2 行目から 2H+1 行目までには、青いマスの配置を出力せよ。

どちらも、紫色のマスの配置と同様のフォーマットで出力せよ。


入力例 1

5 5
.....
.#.#.
.....
.#.#.
.....

出力例 1

.....
#####
#....
#####
.....

.###.
.#.#.
.#.#.
.#.#.
.....

例えば、次のような赤いマスの配置と青いマスの配置のペアが考えられます。


入力例 2

7 13
.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#.###.###.
.............

出力例 2

.............
.###########.
.###.###.###.
.###.###.###.
.###.###.###.
.###.###.###.
.............

.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#########.
.............

例えば、次のような赤いマスの配置と青いマスの配置のペアが考えられます。

Score : 700 points

Problem Statement

Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with H rows and W columns.

Snuke painted some of the cells red in his paper. Here, the cells painted red were 4-connected, that is, it was possible to traverse from any red cell to any other red cell, by moving to vertically or horizontally adjacent red cells only.

Ciel painted some of the cells blue in her paper. Here, the cells painted blue were 4-connected.

Afterwards, they precisely overlaid the two sheets in the same direction. Then, the intersection of the red cells and the blue cells appeared purple.

You are given a matrix of letters a_{ij} (1≤i≤H, 1≤j≤W) that describes the positions of the purple cells. If the cell at the i-th row and j-th column is purple, then a_{ij} is #, otherwise a_{ij} is .. Here, it is guaranteed that no outermost cell is purple. That is, if i=1, H or j = 1, W, then a_{ij} is ..

Find a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation described. It can be shown that a solution always exists.

Constraints

  • 3≤H,W≤500
  • a_{ij} is # or ..
  • If i=1,H or j=1,W, then a_{ij} is ..
  • At least one of a_{ij} is #.

Input

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

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

Output

Print a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation, as follows:

  • The first H lines should describe the positions of the red cells.
  • The following 1 line should be empty.
  • The following H lines should describe the positions of the blue cells.

The description of the positions of the red or blue cells should follow the format of the description of the positions of the purple cells.


Sample Input 1

5 5
.....
.#.#.
.....
.#.#.
.....

Sample Output 1

.....
#####
#....
#####
.....

.###.
.#.#.
.#.#.
.#.#.
.....

One possible pair of the set of the positions of the red cells and the blue cells is as follows:


Sample Input 2

7 13
.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#.###.###.
.............

Sample Output 2

.............
.###########.
.###.###.###.
.###.###.###.
.###.###.###.
.###.###.###.
.............

.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#########.
.............

One possible pair of the set of the positions of the red cells and the blue cells is as follows:

D - Teleporter

Time Limit: 1 sec / Memory Limit: 256 MB

配点 : 800

問題文

高橋王国には N 個の町があります。 町は 1 から N まで番号が振られています。 町 1 は首都です。

それぞれの町にはテレポーターが 1 台ずつ設置されています。 町 i (1≤i≤N) のテレポーターの転送先は町 a_i (1≤a_i≤N) です。 どの町から出発しても、テレポーターを何回か使うことで首都へ辿り着けることが保証されます。

高橋王は正の整数 K が好きです。 わがままな高橋王は、いくつかのテレポーターの転送先を変え、次の条件が成り立つようにしたいと思っています。

  • どの町から出発しても、テレポーターをちょうど K 回使うと、最終的に首都にいる。

条件が成り立つようにするためには、最少でいくつのテレポーターの転送先を変えればよいかを求めてください。

制約

  • 2≤N≤10^5
  • 1≤a_i≤N
  • どの町から出発しても、テレポーターを何回か使うことで首都へ辿り着ける。
  • 1≤K≤10^9

入力

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

N K
a_1 a_2 ... a_N

出力

条件が成り立つようにするためには、最少でいくつのテレポーターの転送先を変えればよいかを出力せよ。


入力例 1

3 1
2 3 1

出力例 1

2

テレポーターの転送先を a = (1,1,1) と変えればよいです。


入力例 2

4 2
1 1 2 2

出力例 2

0

最初から条件が成り立っているので、テレポーターの転送先を変える必要はありません。


入力例 3

8 2
4 1 2 3 1 2 3 4

出力例 3

3

例えば、テレポーターの転送先を a = (1,1,2,1,1,2,2,4) と変えればよいです。

Score : 800 points

Problem Statement

There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.

Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.

King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:

  • Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.

Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.

Constraints

  • 2≤N≤10^5
  • 1≤a_i≤N
  • One can get to the capital from any town by using the Teleporters some number of times.
  • 1≤K≤10^9

Input

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

N K
a_1 a_2 ... a_N

Output

Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.


Sample Input 1

3 1
2 3 1

Sample Output 1

2

Change the destinations of the Teleporters to a = (1,1,1).


Sample Input 2

4 2
1 1 2 2

Sample Output 2

0

There is no need to change the destinations of the Teleporters, since the king's desire is already satisfied.


Sample Input 3

8 2
4 1 2 3 1 2 3 4

Sample Output 3

3

For example, change the destinations of the Teleporters to a = (1,1,2,1,1,2,2,4).

E - Salvage Robots

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1400

問題文

H 行、横 W 列のマス目があります。 上から i (1≤i≤H) 行目、左から j (1≤j≤W) 列目のマスの情報は、文字 a_{ij} によって次のように表されます。

  • . : 空きマスである。
  • o : ロボットが 1 個置かれたマスである。
  • E : 出口のあるマスである。 E はマス目全体にちょうど 1 個含まれる。

高橋君は次の操作を何回か行い、できるだけ多くのロボットを救出しようとしています。

  • 上下左右のうちどれかひとつの向きを選び、すべてのロボットをその向きへ 1 マスだけ移動させる。 このとき、出口のあるマスへ移動したロボットは直ちに救出され、マス目から消える。 また、マス目の外へ移動したロボットは直ちに爆発し、マス目から消える。

高橋君が救出できるロボットの個数の最大値を求めてください。

制約

  • 2≤H,W≤100
  • a_{ij}.oE のどれかである。
  • E はマス目全体にちょうど 1 個含まれる。

入力

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

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

出力

高橋君が救出できるロボットの個数の最大値を出力せよ。


入力例 1

3 3
o.o
.Eo
ooo

出力例 1

3

例えば、左、上、右の順にロボットを移動させればよいです。


入力例 2

2 2
E.
..

出力例 2

0

入力例 3

3 4
o...
o...
oooE

出力例 3

5

右、右、右、下、下の順にロボットを移動させればよいです。


入力例 4

5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo

出力例 4

12

Score : 1400 points

Problem Statement

We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:

  • . : This cell is empty.
  • o : This cell contains a robot.
  • E : This cell contains the exit. E occurs exactly once in the whole grid.

Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:

  • Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.

Find the maximum number of robots that can be salvaged.

Constraints

  • 2≤H,W≤100
  • a_{ij} is ., o or E.
  • E occurs exactly once in the whole grid.

Input

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

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

Output

Print the maximum number of robots that can be salvaged.


Sample Input 1

3 3
o.o
.Eo
ooo

Sample Output 1

3

For example, select left, up, right.


Sample Input 2

2 2
E.
..

Sample Output 2

0

Sample Input 3

3 4
o...
o...
oooE

Sample Output 3

5

Select right, right, right, down, down.


Sample Input 4

5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo

Sample Output 4

12
F - Namori

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 2200

問題文

N 頂点 M 辺の無向グラフがあります。 ただし、N-1≤M≤N であり、グラフは連結です。 また、グラフには自己ループや多重辺はありません。

頂点は 1 から N まで番号が振られており、辺は 1 から M まで番号が振られています。 辺 i は頂点 a_ib_i を結んでいます。

各頂点は白または黒になることができます。 最初、すべての頂点は白です。 高橋君は次の操作を何回か行い、すべての頂点を黒にしようとしています。

  • 隣り合う同色の頂点のペアを選び、それらの色をともに反転する。 すなわち、ともに白ならばともに黒へ変え、ともに黒ならばともに白へ変える。

すべての頂点を黒にすることができるか判定し、できるならば必要な操作回数の最小値を求めてください。

制約

  • 2≤N≤10^5
  • N-1≤M≤N
  • 1≤a_i,b_i≤N
  • グラフには自己ループや多重辺はない。
  • グラフは連結である。

部分点

  • 1500 点分のデータセットでは、M=N-1 が成り立つ。

入力

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

N M
a_1 b_1
a_2 b_2
:
a_M b_M

出力

すべての頂点を黒にすることができるならば、必要な操作回数の最小値を出力せよ。 できないならば、代わりに -1 を出力せよ。


入力例 1

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

出力例 1

5

例えば、図のように操作を行えばよいです。


入力例 2

3 2
1 2
2 3

出力例 2

-1

すべての頂点を黒にすることはできません。


入力例 3

4 4
1 2
2 3
3 4
4 1

出力例 3

2

このケースは部分点のデータセットには含まれません。


入力例 4

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

出力例 4

7

このケースは部分点のデータセットには含まれません。

Score : 2200 points

Problem Statement

You are given an undirected graph with N vertices and M edges. Here, N-1≤M≤N holds and the graph is connected. There are no self-loops or multiple edges in this graph.

The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects vertices a_i and b_i.

The color of each vertex can be either white or black. Initially, all the vertices are white. Snuke is trying to turn all the vertices black by performing the following operation some number of times:

  • Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.

Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.

Constraints

  • 2≤N≤10^5
  • N-1≤M≤N
  • 1≤a_i,b_i≤N
  • There are no self-loops or multiple edges in the given graph.
  • The given graph is connected.

Partial Score

  • In the test set worth 1500 points, M=N-1.

Input

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

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective. Otherwise, print -1 instead.


Sample Input 1

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

Sample Output 1

5

For example, it is possible to perform the operations as shown in the following diagram:


Sample Input 2

3 2
1 2
2 3

Sample Output 2

-1

It is not possible to turn all the vertices black.


Sample Input 3

4 4
1 2
2 3
3 4
4 1

Sample Output 3

2

This case is not included in the test set for the partial score.


Sample Input 4

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

Sample Output 4

7

This case is not included in the test set for the partial score.