A - Darker and Darker

Time Limit: 1 sec / Memory Limit: 1024 MB

配点 : 300

問題文

H 行、横 W 列の白黒に塗られたマス目が与えられます。 マス目の状態は A_{11} から A_{HW}HW 個の文字で表されており、 上から i 行目、左から j 列目にあるマスが黒色のとき A_{ij}#、 上から i 行目、左から j 列目にあるマスが白色のとき A_{ij}. となっています。

すべてのマスが黒色になるまで、以下の操作を繰り返し行います。

  • 辺を共有して隣接するマスの中に、黒色のマスが一つ以上存在するような白色のマスすべてが黒色になる。

何回の操作を行うことになるか求めてください。 ただし、最初に与えられるマス目には少なくとも 1 つ黒色のマスが存在します。

制約

  • 1 ≦ H,W ≦ 1000
  • A_{ij}# または .
  • 与えられるマス目には少なくとも 1 つ黒色のマスが存在する。

入力

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

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

出力

行われる操作の回数を出力せよ。


入力例 1

3 3
...
.#.
...

出力例 1

2

操作を一回行うとマス目の四隅以外が黒色になり、もう一度操作を行うとすべてのマス目が黒色になります。


入力例 2

6 6
..#..#
......
#..#..
......
.#....
....#.

出力例 2

3

Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black. HW characters from A_{11} to A_{HW} represent the colors of the squares. A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

  • Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed. The initial grid has at least one black square.

Constraints

  • 1 \leq H,W \leq 1000
  • A_{ij} is # or ..
  • The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.


Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.


Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3
B - LRUD Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

H 行、横 W 列の長方形上のマス目があります。上から i 行目、左から j 列目のマスを (i,j) と表します。 このマス目の上には一つの駒が置いてあり、最初はマス (s_r,s_c) に置いてあります。

高橋君と青木君はそれぞれ長さ N の文字列を用意してゲームをすることにしました。 高橋君は文字列 S を、青木君は文字列 T を用意し、ST はともに L, R, U, D4 種類の文字からなります。

ゲームは N 回のステップからなります。i 回目のステップは以下のように進行します。

  • まず高橋君が操作を行う。この操作では、駒を S_i の方向に動かす、もしくは、駒を動かさないかのいずれかを行う。
  • 次に青木君が操作を行う。この操作では、駒を T_i の方向に動かす、もしくは、駒を動かさないかのいずれかを行う。

ここで、駒を L, R, U, D の方向に動かすとは、駒がマス (r,c) にあったとき、 それぞれマス (r,c-1), (r,c+1), (r-1,c), (r+1,c) に動かす操作を指します。 ただし、その座標に対応するマスが存在しない場合は、駒をマス目から取り除く操作を指すことにします。 この操作が行われた場合、N 回のステップが終わっていなくても、その時点でゲームは終了します。

高橋君は N 回のステップのいずれかのステップで駒をマス目から取り除きたいです。 一方で、青木君は最終的に駒がマス目上に残ったまま、N 回のステップを終えたいです。 二人が最適に行動したとき、ゲームが終了した時点で駒がマス目上に残っているかどうかを判定してください。

制約

  • 2 ≦ H,W ≦ 2 \times 10^5
  • 2 ≦ N ≦ 2 \times 10^5
  • 1 ≦ s_r ≦ H
  • 1 ≦ s_c ≦ W
  • |S|=|T|=N
  • STL, R, U, D4 種類の文字からなる。

入力

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

H W N
s_r s_c
S
T

出力

ゲームが終了した時点で駒がマス目上に残っているならば YES を、 そうでないならば NO と出力せよ。


入力例 1

2 3 3
2 2
RRL
LUD

出力例 1

YES

ゲームは例えば以下のように進行します。

  • 高橋君が駒を右に動かし、駒は (2,3) に移動する。
  • 青木君が駒を左に動かし、駒は (2,2) に移動する。
  • 高橋君は駒を動かさず、駒の位置は (2,2) のままとなる。
  • 青木君は駒を上に動かし、駒は (1,2) に移動する。
  • 高橋君は駒を左に動かし、駒は (1,1) に移動する。
  • 青木君は駒を動かさず、駒の位置は (1,1) のままとなる。

入力例 2

4 3 5
2 2
UDRRR
LLDUD

出力例 2

NO

入力例 3

5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD

出力例 3

NO

Score : 600 points

Problem Statement

We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left. On this grid, there is a piece, which is initially placed at square (s_r,s_c).

Takahashi and Aoki will play a game, where each player has a string of length N. Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: L, R, U and D.

The game consists of N steps. The i-th step proceeds as follows:

  • First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.
  • Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.

Here, to move the piece in the direction of L, R, U and D, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.

Takahashi wants to remove the piece from the grid in one of the N steps. Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally.

Constraints

  • 2 \leq H,W \leq 2 \times 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq s_r \leq H
  • 1 \leq s_c \leq W
  • |S|=|T|=N
  • S and T consists of the four kinds of letters L, R, U and D.

Input

Input is given from Standard Input in the following format:

H W N
s_r s_c
S
T

Output

If the piece will remain on the grid at the end of the game, print YES; otherwise, print NO.


Sample Input 1

2 3 3
2 2
RRL
LUD

Sample Output 1

YES

Here is one possible progress of the game:

  • Takahashi moves the piece right. The piece is now at (2,3).
  • Aoki moves the piece left. The piece is now at (2,2).
  • Takahashi does not move the piece. The piece remains at (2,2).
  • Aoki moves the piece up. The piece is now at (1,2).
  • Takahashi moves the piece left. The piece is now at (1,1).
  • Aoki does not move the piece. The piece remains at (1,1).

Sample Input 2

4 3 5
2 2
UDRRR
LLDUD

Sample Output 2

NO

Sample Input 3

5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD

Sample Output 3

NO
C - Removing Coins

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

高橋君と青木君は木を用いてゲームをすることにしました。 ゲームに用いる木は N 頂点からなり、各頂点には 1 から N の番号が割り振られています。 また、N-1 本の辺のうち、 i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。

ゲーム開始時、各頂点にはコインが一枚ずつ置いてあります。 高橋君と青木君は高橋君から始めて以下の操作を交互に行い、操作を行えなくなった方が負けになります。

  • コインが置いてある頂点を一つ選び、その頂点 v に置いてあるコインをすべて取り除く。
  • その後、木上に残っているコインをすべて、今置いてある頂点に隣接する頂点のうち v に一番近い頂点に移動させる。

つまり、木上にコインが残っていない状態で手番となった人の負けです。 二人が最適に行動したとき、どちらが勝つか求めてください。

制約

  • 1 ≦ N ≦ 2 \times 10^5
  • 1 ≦ a_i, b_i ≦ N
  • a_i \neq b_i
  • 入力で与えられるグラフは木である。

入力

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

N
a_1 b_1
a_2 b_2
:
a_{N-1} b_{N-1}

出力

先手の高橋君が勝つなら First を、後手の青木君が勝つなら Second を出力せよ。


入力例 1

3
1 2
2 3

出力例 1

First

ゲームは例えば以下のように進行します。

  • 高橋君が頂点 1 からコインを取り除く。操作後は、頂点 1 に一つ、頂点 2 に一つコインがある。
  • 青木君が頂点 2 からコインを取り除く。操作後は、頂点 2 に一つコインがある。
  • 高橋君が頂点 2 からコインを取り除く。操作後は、木上にコインは残っていない。
  • 青木君は木上にコインがない状態で手番となってしまったので、負けとなる。

入力例 2

6
1 2
2 3
2 4
4 6
5 6

出力例 2

Second

入力例 3

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

出力例 3

First

Score : 800 points

Problem Statement

Takahashi and Aoki will play a game on a tree. The tree has N vertices numbered 1 to N, and the i-th of the N-1 edges connects Vertex a_i and Vertex b_i.

At the beginning of the game, each vertex contains a coin. Starting from Takahashi, he and Aoki will alternately perform the following operation:

  • Choose a vertex v that contains one or more coins, and remove all the coins from v.
  • Then, move each coin remaining on the tree to the vertex that is nearest to v among the adjacent vertices of the coin's current vertex.

The player who becomes unable to play, loses the game. That is, the player who takes his turn when there is no coin remaining on the tree, loses the game. Determine the winner of the game when both players play optimally.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq a_i, b_i \leq N
  • a_i \neq b_i
  • The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_{N-1} b_{N-1}

Output

Print First if Takahashi will win, and print Second if Aoki will win.


Sample Input 1

3
1 2
2 3

Sample Output 1

First

Here is one possible progress of the game:

  • Takahashi removes the coin from Vertex 1. Now, Vertex 1 and Vertex 2 contain one coin each.
  • Aoki removes the coin from Vertex 2. Now, Vertex 2 contains one coin.
  • Takahashi removes the coin from Vertex 2. Now, there is no coin remaining on the tree.
  • Aoki takes his turn when there is no coin on the tree and loses.

Sample Input 2

6
1 2
2 3
2 4
4 6
5 6

Sample Output 2

Second

Sample Input 3

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

Sample Output 3

First
D - Complexity

Time Limit: 5 sec / Memory Limit: 512 MB

配点 : 1000

問題文

この問題のメモリ制限はいつもと異なります。注意してください。

各マスが白か黒で塗られている長方形状のマス目に対して、 複雑さ を以下のように定めます。

  • すべてのマスが白、もしくはすべてのマスが黒のとき、複雑さは 0 である。
  • そうでないとき、マス目のいずれかの辺に平行な直線でマス目を 2 つのマス目に分割し、それらのマス目の複雑さを c_1, c_2 とする。 分割の仕方は複数ありうるが、それらにおける \max(c_1, c_2) の最小値を m として、このマス目の複雑さを m+1 とする。

実際に縦 H 行、横 W 列の白黒に塗られたマス目が与えられます。 マス目の状態は A_{11} から A_{HW}HW 個の文字で表されており、 上から i 行目、左から j 列目にあるマスが黒色のとき A_{ij}#、 上から i 行目、左から j 列目にあるマスが白色のとき A_{ij}. となっています。

与えられたマス目の複雑さを求めてください。

制約

  • 1 ≦ H,W ≦ 185
  • A_{ij}# または .

入力

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

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

出力

与えられたマス目の複雑さを出力せよ。


入力例 1

3 3
...
.##
.##

出力例 1

2

1 列目と 2 列目の境目で 2 つのマス目に分割してみます。 1 列目だけからなるマス目の複雑さは 02,3 列目からなるマス目の複雑さは 1 なので、 このマス目の複雑さは 2 以下だと分かります。


入力例 2

6 7
.####.#
#....#.
#....#.
#....#.
.####.#
#....##

出力例 2

4

Score : 1000 points

Problem Statement

Note the unusual memory limit.

For a rectangular grid where each square is painted white or black, we define its complexity as follows:

  • If all the squares are black or all the squares are white, the complexity is 0.
  • Otherwise, divide the grid into two subgrids by a line parallel to one of the sides of the grid, and let c_1 and c_2 be the complexities of the subgrids. There can be multiple ways to perform the division, and let m be the minimum value of \max(c_1, c_2) in those divisions. The complexity of the grid is m+1.

You are given a grid with H horizontal rows and W vertical columns where each square is painted white or black. HW characters from A_{11} to A_{HW} represent the colors of the squares. A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

Find the complexity of the given grid.

Constraints

  • 1 \leq H,W \leq 185
  • A_{ij} is # or ..

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the complexity of the given grid.


Sample Input 1

3 3
...
.##
.##

Sample Output 1

2

Let us divide the grid by the boundary line between the first and second columns. The subgrid consisting of the first column has the complexity of 0, and the subgrid consisting of the second and third columns has the complexity of 1, so the whole grid has the complexity of at most 2.


Sample Input 2

6 7
.####.#
#....#.
#....#.
#....#.
.####.#
#....##

Sample Output 2

4
E - Go around a Circle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1500

問題文

円周が N 個の点によって N 等分され、それぞれが赤か青のいずれかで塗られているような円が、 RB からなる長さ M の文字列 S をすべての点から生成するとは、以下の条件を満たすことを指します。

  • 円周上の N 個の点のうち 1 つを任意に選び、その点上に駒を置く。
  • 駒を時計回り、または反時計回りに隣合う点まで動かすことを M 回繰り返す。
  • このとき最初にどの点を選んだとしても、うまく動かす向きを定めることで、i 回目に駒が通る円弧の色が S_i であるようにできる。

ただし、S_iR のとき赤を、B のとき青を指すものとします。 また駒を動かす向きは、最初に選ぶ点ごとに変えられることに注意してください。

実際に RB からなる長さ M の文字列 S が与えられます。 円周が N 等分されている円の各円弧を赤または青のいずれかで塗る 2^N 通りの方法のうち、 S をすべての点から生成するような塗り方の個数を 10^9+7 で割ったあまりを求めてください。

ただし、回転して一致するような塗り方も区別して数えます。

制約

  • 2 ≦ N ≦ 2 \times 10^5
  • 1 ≦ M ≦ 2 \times 10^5
  • |S|=M
  • S_iR または B

入力

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

N M
S

出力

条件を満たすような各円弧の塗り方の個数を 10^9+7 で割ったあまりを出力せよ。


入力例 1

4 7
RBRRBRR

出力例 1

2

赤と青が交互に塗られているときのみ条件を満たします。 なので、このケースの答えは 2 となります。


入力例 2

3 3
BBB

出力例 2

4

入力例 3

12 10
RRRRBRRRRB

出力例 3

78

Score : 1500 points

Problem Statement

Consider a circle whose perimeter is divided by N points into N arcs of equal length, and each of the arcs is painted red or blue. Such a circle is said to generate a string S from every point when the following condition is satisfied:

  • We will arbitrarily choose one of the N points on the perimeter and place a piece on it.
  • Then, we will perform the following move M times: move the piece clockwise or counter-clockwise to an adjacent point.
  • Here, whatever point we choose initially, it is always possible to move the piece so that the color of the i-th arc the piece goes along is S_i, by properly deciding the directions of the moves.

Assume that, if S_i is R, it represents red; if S_i is B, it represents blue. Note that the directions of the moves can be decided separately for each choice of the initial point.

You are given a string S of length M consisting of R and B. Out of the 2^N ways to paint each of the arcs red or blue in a circle whose perimeter is divided into N arcs of equal length, find the number of ways resulting in a circle that generates S from every point, modulo 10^9+7.

Note that the rotations of the same coloring are also distinguished.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • |S|=M
  • S_i is R or B.

Input

Input is given from Standard Input in the following format:

N M
S

Output

Print the number of ways to paint each of the arcs that satisfy the condition, modulo 10^9+7.


Sample Input 1

4 7
RBRRBRR

Sample Output 1

2

The condition is satisfied only if the arcs are alternately painted red and blue, so the answer here is 2.


Sample Input 2

3 3
BBB

Sample Output 2

4

Sample Input 3

12 10
RRRRBRRRRB

Sample Output 3

78
F - Adding Edges

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 2200

問題文

N 頂点からなる木 TN 頂点 M 辺からなる無向グラフ G が与えられます。 それぞれの各頂点には 1 から N の番号が割り振られています。 TN-1 本の辺のうち、i 本目の辺は頂点 a_i と頂点 b_i を繋いでいます。 GM 本の辺のうち、j 本目の辺は頂点 c_j と頂点 d_j を繋いでいます。

G に対して以下の操作を繰り返し行うことで、G に辺を追加することを考えます。

  • 3 つの整数 a,b,c であって、G の頂点 ab 間と bc 間に辺があり、ac 間に辺がないようなものを選ぶ。 T のある単純パス上に頂点 a,b,c が何らかの順序ですべて含まれるとき、G の頂点 ac 間に辺を追加する。

これ以上辺を追加することができなくなったとき、G の辺の数はいくつになるか求めてください。 この数はどのように操作を行っても変わらないことが示せます。

制約

  • 2 ≦ N ≦ 2000
  • 1 ≦ M ≦ 2000
  • 1 ≦ a_i, b_i ≦ N
  • a_i \neq b_i
  • 1 ≦ c_j, d_j ≦ N
  • c_j \neq d_j
  • G は多重辺を含まない
  • T は木である

入力

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

N M
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_M d_M

出力

最終的な G の辺の数を出力せよ。


入力例 1

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

出力例 1

6

以下の順で辺を追加することで 6 本まで辺を増やすことができます。

  • (a,b,c)=(1,5,4) とし、頂点 1 と頂点 4 の間に辺を追加する。
  • (a,b,c)=(1,5,2) とし、頂点 1 と頂点 2 の間に辺を追加する。
  • (a,b,c)=(2,1,4) とし、頂点 2 と頂点 4 の間に辺を追加する。

入力例 2

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

出力例 2

11

入力例 3

13 11
6 13
1 2
5 1
8 4
9 7
12 2
10 11
1 9
13 7
13 11
8 10
3 8
4 13
8 12
4 7
2 3
5 11
1 4
2 11
8 10
3 5
6 9
4 10

出力例 3

27

Score : 2200 points

Problem Statement

You are given a tree T with N vertices and an undirected graph G with N vertices and M edges. The vertices of each graph are numbered 1 to N. The i-th of the N-1 edges in T connects Vertex a_i and Vertex b_i, and the j-th of the M edges in G connects Vertex c_j and Vertex d_j.

Consider adding edges to G by repeatedly performing the following operation:

  • Choose three integers a, b and c such that G has an edge connecting Vertex a and b and an edge connecting Vertex b and c but not an edge connecting Vertex a and c. If there is a simple path in T that contains all three of Vertex a, b and c in some order, add an edge in G connecting Vertex a and c.

Print the number of edges in G when no more edge can be added. It can be shown that this number does not depend on the choices made in the operation.

Constraints

  • 2 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • 1 \leq a_i, b_i \leq N
  • a_i \neq b_i
  • 1 \leq c_j, d_j \leq N
  • c_j \neq d_j
  • G does not contain multiple edges.
  • T is a tree.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_M d_M

Output

Print the final number of edges in G.


Sample Input 1

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

Sample Output 1

6

We can have at most six edges in G by adding edges as follows:

  • Let (a,b,c)=(1,5,4) and add an edge connecting Vertex 1 and 4.
  • Let (a,b,c)=(1,5,2) and add an edge connecting Vertex 1 and 2.
  • Let (a,b,c)=(2,1,4) and add an edge connecting Vertex 2 and 4.

Sample Input 2

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

Sample Output 2

11

Sample Input 3

13 11
6 13
1 2
5 1
8 4
9 7
12 2
10 11
1 9
13 7
13 11
8 10
3 8
4 13
8 12
4 7
2 3
5 11
1 4
2 11
8 10
3 5
6 9
4 10

Sample Output 3

27