A - Cookie Exchanges

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

高橋君と青木君とすぬけ君はそれぞれクッキーを A,B,C 個持っています。

この 3 人はお互いにクッキーを交換することにしました。具体的には、以下の操作を繰り返します。

  • 3 人は同時に、各々が持っているクッキーを半分ずつに分けて、残りの 2 人にそれぞれ一方を渡す。

ただし、誰かの持っているクッキーの個数が奇数個になったら、そこで操作を繰り返すのをやめます。

さて、クッキーの交換は何回行うことができるでしょうか。 ただし、無限に続けられる場合もあることに注意してください。

制約

  • 1 ≦ A,B,C ≦ 10^9

入力

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

A B C

出力

3 人がクッキーの交換を行うことができる回数を出力せよ。ただし、無限に続けられる場合は -1 を出力せよ。


入力例 1

4 12 20

出力例 1

3

はじめ、高橋君と青木君とすぬけ君はそれぞれクッキーを 4,12,20 個持っており、

  • 1 回目の操作後は、高橋君と青木君とすぬけ君はそれぞれクッキーを 16,12,8 個持っている。
  • 2 回目の操作後は、高橋君と青木君とすぬけ君はそれぞれクッキーを 10,12,14 個持っている。
  • 3 回目の操作後は、高橋君と青木君とすぬけ君はそれぞれクッキーを 13,12,11 個持っている。

3 回目の操作後に高橋君とすぬけ君の持っているクッキーの個数が奇数個になるので、求める回数は 3 回となります。


入力例 2

14 14 14

出力例 2

-1

入力例 3

454 414 444

出力例 3

1

Score : 300 points

Problem Statement

Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:

  • Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.

This action will be repeated until there is a person with odd number of cookies in hand.

How many times will they repeat this action? Note that the answer may not be finite.

Constraints

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

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the number of times the action will be performed by the three people, if this number is finite. If it is infinite, print -1 instead.


Sample Input 1

4 12 20

Sample Output 1

3

Initially, Takahashi, Aoki and Snuke have 4, 12 and 20 cookies. Then,

  • After the first action, they have 16, 12 and 8.
  • After the second action, they have 10, 12 and 14.
  • After the third action, they have 13, 12 and 11.

Now, Takahashi and Snuke have odd number of cookies, and therefore the answer is 3.


Sample Input 2

14 14 14

Sample Output 2

-1

Sample Input 3

454 414 444

Sample Output 3

1
B - Unplanned Queries

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

高橋君は木の問題が苦手です。そこで、青木君は高橋君の練習相手になってあげることにしました。

まず、高橋君は N 頂点からなる木を用意し、頂点に 1 から N の番号を付けました。 そして、各辺に 0 と書きました。

次に、青木君は高橋君に M 個のクエリを与えました。i 個目のクエリは以下のような内容です。

  • 頂点 a_i と頂点 b_i を結ぶパス上の辺すべてに対して、書かれている数を 1 増やす。

全てのクエリを終えた後、高橋君は青木君にどの辺を見ても書かれている数が偶数になったと伝えました。 しかし、青木君は最初に高橋君が用意していた木を確認していなかったので、 高橋君が正しくクエリを処理できたか分かりませんでした。

青木君を助けるために、高橋くんの言う性質を満たす木が存在するかどうかを判定してください。

制約

  • 2 ≦ N ≦ 10^5
  • 1 ≦ M ≦ 10^5
  • 1 ≦ a_i,b_i ≦ N
  • a_i ≠ b_i

入力

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

N M
a_1 b_1
:
a_M b_M

出力

高橋くんの言う性質を満たす木が存在するならば YES を、存在しないならば NO を出力せよ。


入力例 1

4 4
1 2
2 4
1 3
3 4

出力例 1

YES

例えば、頂点 1 と頂点 2,3,4 が辺で結ばれているような木を高橋君が持っている場合は、高橋くんの言っていることは正しいです。 この場合、クエリをすべて終えた後各辺に書かれている数はどれも 2 になります。


入力例 2

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

出力例 2

NO

Score : 500 points

Problem Statement

Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.

First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.

Then, Aoki gave him M queries. The i-th of them is as follows:

  • Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.

After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.

Determine whether there exists a tree that has the property mentioned by Takahashi.

Constraints

  • 2 ≤ N ≤ 10^5
  • 1 ≤ M ≤ 10^5
  • 1 ≤ a_i,b_i ≤ N
  • a_i ≠ b_i

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

Print YES if there exists a tree that has the property mentioned by Takahashi; print NO otherwise.


Sample Input 1

4 4
1 2
2 4
1 3
3 4

Sample Output 1

YES

For example, Takahashi's graph has the property mentioned by him if it has the following edges: 1-2, 1-3 and 1-4. In this case, the number written at every edge will become 2.


Sample Input 2

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

Sample Output 2

NO
C - Closed Rooms

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 700

問題文

高橋君は建物の中に閉じ込められてしまいました。

この建物は HW 列に並んだ H×W 個の部屋からなり、上から i 行目、左から j 列目の部屋は (i,j) で表され、その部屋の状態は A_{i,j} で表されています。 A_{i,j}= # の場合は、この部屋は閉じられており、A_{i,j}= . の場合は、この部屋には自由に出入りできます。 そして、 A_{i,j}= S となる部屋が高橋君の今いる部屋です。ただし、高橋君が今いる部屋も自由に出入りできる部屋です。

また、1 行目、1 列目、H 行目、W 列目のいずれかに含まれる部屋は建物の外につながっており、 それ以外の各部屋 (i,j)4 つの部屋 (i-1,j) , (i+1,j) , (i,j-1) , (i,j+1) と隣接しています。

高橋君はこの建物から脱出するために魔法を使うことにしました。一回の魔法で高橋君は以下の操作ができます。

  • 高橋君は今いる部屋から隣り合う部屋に移動することを K 回まで繰り返す。ただし、閉じられている部屋には移動することはできない。
  • その後、閉じられている部屋を K 個まで選び、それらを開いた状態にする。それらの部屋は以降自由に出入りできるようになる。

ただし、これらの操作では、全く動かなかったり、閉じられている部屋があっても開かなかったりしてもよいです。

高橋君の目標は建物の外につながっている部屋のいずれかにたどり着くことです。そのために必要な魔法の回数の最小値を求めてください。

ただし、はじめに高橋君がいる部屋は建物の外とはつながっていないことが保証されています。

制約

  • 3 ≦ H ≦ 800
  • 3 ≦ W ≦ 800
  • 1 ≦ K ≦ H×W
  • A_{i,j}# , . , S のいずれかである。
  • A_{i,j}= S となる (i,j) は一意に存在し、2 ≦ i ≦ H-1 , 2 ≦ j ≦ W-1 を満たす。

入力

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

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

出力

高橋君が必要な魔法の回数の最小値を出力せよ。


入力例 1

3 3 3
#.#
#S.
###

出力例 1

1

高橋君は最初の魔法で部屋 (1,2) に移動することができるので、1 回の魔法で十分です。


入力例 2

3 3 3
###
#S#
###

出力例 2

2

高橋君は最初の魔法では移動することができないですが、部屋 (1,2)1 回目の魔法で開けることができます。 そして、次の魔法で部屋 (1,2) に移動することで、2 回の魔法で目標を達成できます。


入力例 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

出力例 3

2

Score : 700 points

Problem Statement

Takahashi is locked within a building.

This building consists of H×W rooms, arranged in H rows and W columns. We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered. Takahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit. Each of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

  • Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.
  • Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

It is guaranteed that Takahashi is initially at a room without an exit.

Constraints

  • 3 ≤ H ≤ 800
  • 3 ≤ W ≤ 800
  • 1 ≤ K ≤ H×W
  • Each A_{i,j} is # , . or S.
  • There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.

Input

Input is given from Standard Input in the following format:

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

Output

Print the minimum necessary number of casts.


Sample Input 1

3 3 3
#.#
#S.
###

Sample Output 1

1

Takahashi can move to room (1,2) in one cast.


Sample Input 2

3 3 3
###
#S#
###

Sample Output 2

2

Takahashi cannot move in the first cast, but can unlock room (1,2). Then, he can move to room (1,2) in the next cast, achieving the objective in two casts.


Sample Input 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

Sample Output 3

2
D - Black and White Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 900

問題文

N 頂点からなる木があり、頂点には 1 から N の番号がついています。 また、 N-1 本の辺の内、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。

はじめ、どの頂点にも色がついていません。

高橋君と青木君は各頂点に色を塗ってゲームをします。ゲームでは高橋君から始めて交互に以下の操作を繰り返します。

  • 頂点の中から、まだ色がついていない頂点を一つ選ぶ。
  • その後、高橋君ならその頂点を白色に、青木君ならその頂点を黒色に塗る。

次にすべての頂点に色がついた後、高橋君と青木君は以下の手順を一度だけ行います。

  • 黒色の頂点に隣接している白色の頂点をすべて黒色に塗りかえる。

ただし、この操作はある頂点から順に操作を行っていくわけではなく、当該頂点すべてに対して同時に行うことに注意してください。

最終的に白色の頂点が残っていれば高橋君の勝ちであり、全て黒色の頂点であれば青木君の勝ちです。 二人が最適に行動したとき、どちらが勝つか求めてください。

制約

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

入力

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

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

出力

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


入力例 1

3
1 2
2 3

出力例 1

First

ゲームの一例を示す。

  • 高橋君がまず頂点 2 を白色に塗る。
  • その後、青木君が頂点 1 を黒色に塗る。
  • 最後に高橋君が頂点 3 を白色に塗る。

このように進んだ場合、最後の操作で頂点 1,2,3 の色がそれぞれ黒、黒、白となるので、高橋君の勝ちとなる。


入力例 2

4
1 2
2 3
2 4

出力例 2

First

入力例 3

6
1 2
2 3
3 4
2 5
5 6

出力例 3

Second

Score : 900 points

Problem Statement

There is a tree with N vertices numbered 1 through N. The i-th of the N-1 edges connects vertices a_i and b_i.

Initially, each vertex is uncolored.

Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi:

  • Select a vertex that is not painted yet.
  • If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki.

Then, after all the vertices are colored, the following procedure takes place:

  • Repaint every white vertex that is adjacent to a black vertex, in black.

Note that all such white vertices are repainted simultaneously, not one at a time.

If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins. Determine the winner of the game, assuming that both persons play optimally.

Constraints

  • 2 ≤ N ≤ 10^5
  • 1 ≤ a_i,b_i ≤ N
  • a_i ≠ b_i
  • The input graph is a tree.

Input

Input is given from Standard Input in the following format:

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

Output

Print First if Takahashi wins; print Second if Aoki wins.


Sample Input 1

3
1 2
2 3

Sample Output 1

First

Below is a possible progress of the game:

  • First, Takahashi paint vertex 2 white.
  • Then, Aoki paint vertex 1 black.
  • Lastly, Takahashi paint vertex 3 white.

In this case, the colors of vertices 1, 2 and 3 after the final procedure are black, black and white, resulting in Takahashi's victory.


Sample Input 2

4
1 2
2 3
2 4

Sample Output 2

First

Sample Input 3

6
1 2
2 3
3 4
2 5
5 6

Sample Output 3

Second
E - Blue and Red Tree

Time Limit: 6 sec / Memory Limit: 256 MB

配点 : 1400

問題文

N 頂点からなる木があり、頂点には 1 から N の番号がついています。 また、 N-1 本の辺の内、i 番目の辺は頂点 a_i と頂点 b_i を結んでいます。

はじめ、各辺は青色に塗られています。 そこで、高橋君は以下の操作を N-1 回行い、赤色の木に作り替えることにしました。

  • 青色の辺のみからなるパスを一つ選び、そのパス上の辺を一つ取り除く。
  • その後、初めに選んだパスの両端点間に赤色の辺を追加する。

最終的に、各 i に対し、頂点 c_i と頂点 d_i を結ぶ赤い辺が存在するような N 頂点の木に作り替えたいです。

これが可能であるかどうか判定してください。

制約

  • 2 ≦ N ≦ 10^5
  • 1 ≦ a_i,b_i,c_i,d_i ≦ N
  • a_i ≠ b_i
  • c_i ≠ d_i
  • 入力で与えられるグラフはどちらも木である。

入力

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

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}

出力

高橋君が木を目標の木に作り替えられるならば YES を、作り替えられないならば NO を出力せよ。


入力例 1

3
1 2
2 3
1 3
3 2

出力例 1

YES

高橋君は以下の手順で目標の赤い木を作ることができます。

  • まず、頂点 1 と頂点 3 を結ぶパスを選び、青い辺 1-2 を削除する。そして、赤い辺 1-3 を追加する。
  • 次に、頂点 2 と頂点 3 を結ぶパスを選び、青い辺 2-3 を削除する。そして、赤い辺 2-3 を追加する。

入力例 2

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

出力例 2

YES

入力例 3

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

出力例 3

NO

Score : 1400 points

Problem Statement

There is a tree with N vertices numbered 1 through N. The i-th of the N-1 edges connects vertices a_i and b_i.

Initially, each edge is painted blue. Takahashi will convert this blue tree into a red tree, by performing the following operation N-1 times:

  • Select a simple path that consists of only blue edges, and remove one of those edges.
  • Then, span a new red edge between the two endpoints of the selected path.

His objective is to obtain a tree that has a red edge connecting vertices c_i and d_i, for each i.

Determine whether this is achievable.

Constraints

  • 2 ≤ N ≤ 10^5
  • 1 ≤ a_i,b_i,c_i,d_i ≤ N
  • a_i ≠ b_i
  • c_i ≠ d_i
  • Both input graphs are trees.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}

Output

Print YES if the objective is achievable; print NO otherwise.


Sample Input 1

3
1 2
2 3
1 3
3 2

Sample Output 1

YES

The objective is achievable, as below:

  • First, select the path connecting vertices 1 and 3, and remove a blue edge 1-2. Then, span a new red edge 1-3.
  • Next, select the path connecting vertices 2 and 3, and remove a blue edge 2-3. Then, span a new red edge 2-3.

Sample Input 2

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

Sample Output 2

YES

Sample Input 3

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

Sample Output 3

NO
F - Strange Sorting

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 2400

問題文

高橋君はソートをするのが大好きです。

そこで、高橋君は 1 から N の順列 (p_1,p_2,...,p_N) を用意し、この順列が (1,2,...,N) になるまで以下の操作を繰り返すことにしました。

  • まず、順列の各 i 項目に対して、1 項目から i 項目までの最大値が i 項目自身であるような項を「高い項」、そうでない項を「低い項」とする。
  • そして、今の順列で並んでいる順に、高い項に現れる数を a_1,a_2,...,a_k 、低い項に現れる数を b_1,b_2,...,b_{N-k} とする。
  • 最後に、順列を (b_1,b_2,...,b_{N-k},a_1,a_2,...,a_k) と並び替える。

さて、高橋君がソートを完了するまでに何回の操作が必要か求めてください。

制約

  • 1 ≦ N ≦ 2×10^5
  • (p_1,p_2,...,p_N)1 から N の順列である。

入力

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

N
p_1 p_2 ... p_N

出力

高橋君がソートを完了するまでにかかる操作の回数を出力せよ。


入力例 1

5
3 5 1 2 4

出力例 1

3

高橋君ははじめ順列 (3,5,1,2,4) を持っており、各操作後、順列は以下のようになる。

  • 1,2 項目が高い項であり、3,4,5 項目が低い項なので、次の順列は (1,2,4,3,5) になる。
  • 1,2,3,5 項目が高い項であり、4 項目が低い項なので、次の順列は (3,1,2,4,5) になる。
  • 1,4,5 項目が高い項であり、2,3 項目が低い項なので、次の順列は (1,2,3,4,5) になる。

入力例 2

5
5 4 3 2 1

出力例 2

4

入力例 3

10
2 10 5 7 3 6 4 9 8 1

出力例 3

6

Score : 2400 points

Problem Statement

Takahashi loves sorting.

He has a permutation (p_1,p_2,...,p_N) of the integers from 1 through N. Now, he will repeat the following operation until the permutation becomes (1,2,...,N):

  • First, we will define high and low elements in the permutation, as follows. The i-th element in the permutation is high if the maximum element between the 1-st and i-th elements, inclusive, is the i-th element itself, and otherwise the i-th element is low.
  • Then, let a_1,a_2,...,a_k be the values of the high elements, and b_1,b_2,...,b_{N-k} be the values of the low elements in the current permutation, in the order they appear in it.
  • Lastly, rearrange the permutation into (b_1,b_2,...,b_{N-k},a_1,a_2,...,a_k).

How many operations are necessary until the permutation is sorted?

Constraints

  • 1 ≤ N ≤ 2×10^5
  • (p_1,p_2,...,p_N) is a permutation of the integers from 1 through N.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print the number of operations that are necessary until the permutation is sorted.


Sample Input 1

5
3 5 1 2 4

Sample Output 1

3

The initial permutation is (3,5,1,2,4), and each operation changes it as follows:

  • In the first operation, the 1-st and 2-nd elements are high, and the 3-rd, 4-th and 5-th are low. The permutation becomes: (1,2,4,3,5).
  • In the second operation, the 1-st, 2-nd, 3-rd and 5-th elements are high, and the 4-th is low. The permutation becomes: (3,1,2,4,5).
  • In the third operation, the 1-st, 4-th and 5-th elements are high, and the 2-nd and 3-rd and 5-th are low. The permutation becomes: (1,2,3,4,5).

Sample Input 2

5
5 4 3 2 1

Sample Output 2

4

Sample Input 3

10
2 10 5 7 3 6 4 9 8 1

Sample Output 3

6