A - Equivalent Resistance

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

電気回路において、抵抗値が R_1 の抵抗と抵抗値が R_2 の抵抗を並列につないだとき、合成抵抗 R_3 は以下の関係式から求めることが出来ます。

  • \frac{1}{R_1} + \frac{1}{R_2} = \frac{1}{R_3}

R_1R_2 が与えられるので R_3 を求めてください。

制約

  • 1 \leq R_1, R_2 \leq 100
  • R_1, R_2 は整数

入力

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

R_1 R_2

出力

R_3 の値を出力せよ。

絶対誤差または相対誤差が 10^{-6} 以下ならば正解となる。


入力例 1

2 3

出力例 1

1.2000000000

入力例 2

100 99

出力例 2

49.7487437186

Score : 100 points

Problem Statement

In an electric circuit, when two resistors R_1 and R_2 are connected in parallel, the equivalent resistance R_3 can be derived from the following formula:

  • \frac{1}{R_1} + \frac{1}{R_2} = \frac{1}{R_3}

Given R_1 and R_2, find R_3.

Constraints

  • 1 \leq R_1, R_2 \leq 100
  • R_1 and R_2 are integers.

Input

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

R_1 R_2

Output

Print the value of R_3.

The output is considered correct if the absolute or relative error is at most 10^{-6}.


Sample Input 1

2 3

Sample Output 1

1.2000000000

Sample Input 2

100 99

Sample Output 2

49.7487437186
B - Mirror String

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

b, d, p, q4 種類の文字から構成される文字列 S が与えられます。 S が「鏡文」かどうかを判定してください。

ここで、「鏡文」というのは以下の操作を文字列 S に施したときに、元と同じ文字列が得られるような文字列 S のことです。

  1. S の順序を逆転する。

  2. bd に、db に、pq に、qp に置換する。

制約

  • 1 \leq |S| \leq 10^5
  • Sb, d, p, q4 種類の文字のみから構成される。

入力

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

S

出力

S が「鏡文」ならば Yes を、そうでないならば No を出力せよ。


入力例 1

pdbq

出力例 1

Yes

入力例 2

ppqb

出力例 2

No

Score : 100 points

Problem Statement

You are given a string S consisting of letters b, d, p and q. Determine whether S is a mirror string.

Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S:

  1. Reverse the order of the characters in S.

  2. Replace each occurrence of b by d, d by b, p by q, and q by p, simultaneously.

Constraints

  • 1 \leq |S| \leq 10^5
  • S consists of letters b, d, p, and q.

Input

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

S

Output

If S is a mirror string, print Yes. Otherwise, print No.


Sample Input 1

pdbq

Sample Output 1

Yes

Sample Input 2

ppqb

Sample Output 2

No
C - Kode Festival

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

「硬度フェスティバル」は毎年開催される、世界で一番硬い石を決める大会です。

今年の硬度フェスティバルには 2^N 個の石が参加します。 i 番目の石の硬度は A_i です。

大会では石をトーナメント形式でぶつけ合って、最硬の石を決めます。

硬度 X の石と硬度 Y の石をぶつけると以下のような結果になります。

  • X > Y のとき: 硬度が Y だった石は砕けて、 硬度が X だった石の硬度は X-Y になります。 このとき硬度が X だった石が勝ち残ります。

  • X = Y のとき: どちらかの石が砕けます。もう片方の石が硬度が元と変わらないまま残ります。このとき砕けなかった方の石が勝ち残ります。

  • X < Y のとき: 硬度が X だった石は砕けて、 硬度が Y だった石の硬度は Y-X になります。 このとき硬度が Y だった石が勝ち残ります。

2^N 個の石は以下のようなトーナメント形式で勝負をします。

  1. (1 番目の石、2 番目の石)、(3 番目の石、4 番目の石)、... の組み合わせでぶつけ合う。

  2. ((1, 2) の勝ち残り、(3, 4) の勝ち残り)、((5, 6) の勝ち残り、(7, 8) の勝ち残り)、... の組み合わせでぶつけ合う。

  3. 同様に、勝ち残りが 1 つだけになるまで続ける。

最後まで勝ち残る石の、最後の時点での硬度を求めてください。

制約

  • 1 \leq N \leq 18
  • 1 \leq A_i \leq 10^9
  • A_i は整数である。

入力

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

N
A_1
A_2
:
A_{2^N}

出力

最後まで勝ち残る石の、最後の時点での硬度を出力せよ。


入力例 1

2
1
3
10
19

出力例 1

7

入力例 2

3
1
3
2
4
6
8
100
104

出力例 2

2

Score : 100 points

Problem Statement

Kode Festival is an anual contest where the hardest stone in the world is determined. (Kode is a Japanese word for "hardness".)

This year, 2^N stones participated. The hardness of the i-th stone is A_i.

In the contest, stones are thrown at each other in a knockout tournament.

When two stones with hardness X and Y are thrown at each other, the following will happen:

  • When X > Y: The stone with hardness Y will be destroyed and eliminated. The hardness of the stone with hardness X will become X-Y.

  • When X = Y: One of the stones will be destroyed and eliminated. The hardness of the other stone will remain the same.

  • When X < Y: The stone with hardness X will be destroyed and eliminated. The hardness of the stone with hardness Y will become Y-X.

The 2^N stones will fight in a knockout tournament as follows:

  1. The following pairs will fight: (the 1-st stone versus the 2-nd stone), (the 3-rd stone versus the 4-th stone), ...

  2. The following pairs will fight: (the winner of (1-st versus 2-nd) versus the winner of (3-rd versus 4-th)), (the winner of (5-th versus 6-th) versus the winner of (7-th versus 8-th)), ...

  3. And so forth, until there is only one stone remaining.

Determine the eventual hardness of the last stone remaining.

Constraints

  • 1 \leq N \leq 18
  • 1 \leq A_i \leq 10^9
  • A_i is an integer.

Input

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

N
A_1
A_2
:
A_{2^N}

Output

Print the eventual hardness of the last stone remaining.


Sample Input 1

2
1
3
10
19

Sample Output 1

7

Sample Input 2

3
1
3
2
4
6
8
100
104

Sample Output 2

2
D - Magic Square 2

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

3 マス × 3 マスの方眼状のマスの中に整数が書かれているものを方陣と呼びます。

タテ、ヨコ、ナナメに並んだ 3 つの数の総和がどれも等しい方陣を魔方陣と呼びます。

ある魔方陣について、以下のような 3 マスに関する情報が与えられます。

  • 一番左の列の一番上には整数 A が書かれている
  • 中央の列の一番上には整数 B が書かれている
  • 中央の列の中央には整数 C が書かれている

これらの情報から、元の魔方陣の残りのマスに書かれている整数を全て求めてください。

なお、条件をみたすような魔方陣は必ず 1 つだけ存在することが保証されています。

制約

  • 0 \leq A, B, C \leq 100

入力

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

A
B
C

各整数の意味は問題文中のとおりである。

出力

以下の形式で元の魔方陣を出力せよ。

X_{1,1} X_{1,2} X_{1,3}
X_{2,1} X_{2,2} X_{2,3}
X_{3,1} X_{3,2} X_{3,3}

X_{i,j} には、元の魔方陣の上から i 行、左から j 列の場所のマスに書かれた整数を出力せよ。


入力例 1

8
3
5

出力例 1

8 3 4
1 5 9
6 7 2

入力例 2

1
1
1

出力例 2

1 1 1
1 1 1
1 1 1

Score : 100 points

Problem Statement

A 3×3 grid with a integer written in each square, is called a magic square if and only if the integers in each row, the integers in each column, and the integers in each diagonal (from the top left corner to the bottom right corner, and from the top right corner to the bottom left corner), all add up to the same sum.

You are given the integers written in the following three squares in a magic square:

  • The integer A at the upper row and left column
  • The integer B at the upper row and middle column
  • The integer C at the middle row and middle column

Determine the integers written in the remaining squares in the magic square.

It can be shown that there exists a unique magic square consistent with the given information.

Constraints

  • 0 \leq A, B, C \leq 100

Input

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

A
B
C

Output

Output the integers written in the magic square, in the following format:

X_{1,1} X_{1,2} X_{1,3}
X_{2,1} X_{2,2} X_{2,3}
X_{3,1} X_{3,2} X_{3,3}

where X_{i,j} is the integer written in the square at the i-th row and j-th column.


Sample Input 1

8
3
5

Sample Output 1

8 3 4
1 5 9
6 7 2

Sample Input 2

1
1
1

Sample Output 2

1 1 1
1 1 1
1 1 1
E - Segment on Grid Paper

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

高橋君は方眼紙の上に線分を書くことにしました。

方眼紙のあるマスを起点として、右に x マス、上に y マス進んだところにあるマスをマス (x, y) と呼びます。

マス (A, B) の左下の点と マス (C, D) の左下の点を結んで線分を書くとき、線分が横切るマスの個数を求めてください。

ただし、線分があるマスの内側(境界は含まない)を通るとき、「線分がそのマスを横切る」と言います。

制約

  • 1 \leq A, B, C, D \leq 10^9
  • A \neq C もしくは B \neq D の少なくとも一方が成り立つ。

入力

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

A B C D

出力

線分が横切るマスの個数を出力せよ。


入力例 1

1 1 3 4

出力例 1

4

入力例 2

2 3 10 7

出力例 2

8

Score : 100 points

Problem Statement

Takahashi is drawing a segment on grid paper.

From a certain square, a square that is x squares to the right and y squares above, is denoted as square (x, y).

When Takahashi draws a segment connecting the lower left corner of square (A, B) and the lower left corner of square (C, D), find the number of the squares crossed by the segment.

Here, the segment is said to cross a square if the segment has non-empty intersection with the region within the square, excluding the boundary.

Constraints

  • 1 \leq A, B, C, D \leq 10^9
  • At least one of A \neq C and B \neq D holds.

Input

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

A B C D

Output

Print the number of the squares crossed by the segment.


Sample Input 1

1 1 3 4

Sample Output 1

4

Sample Input 2

2 3 10 7

Sample Output 2

8
F - Trichotomy

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

長さが正の整数の紐があります。以下の操作を紐の長さが 2 以下になるまで続けます。

  • 操作: 紐を 2 箇所で切り、長さが正の整数である紐 3 つに分ける。 この中で最長のもの 1 つと最短のもの 1 つを捨てる。

長さ N の紐からこの操作を始めたときに、この操作を続けることが出来る回数の最大値を f(N) とします。

正整数 X が与えられるので、f(N)=X となる最大の整数 N を求めてください。

制約

  • 1 \leq X \leq 40

入力

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

X

出力

f(N) = X となる最大の整数 N の値を出力せよ。


入力例 1

2

出力例 1

14

Score : 100 points

Problem Statement

We have a cord whose length is a positive integer. We will perform the following condition until the length of the cord becomes at most 2:

  • Operation: Cut the rope at two positions to obtain three cords, each with a length of a positive integer. Among these, discard one with the longest length and one with the shortest length, and keep the remaining one.

Let f(N) be the maximum possible number of times to perform this operation, starting with a cord with the length N.

You are given a positive integer X. Find the maximum integer N such that f(N)=X.

Constraints

  • 1 \leq X \leq 40

Input

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

X

Output

Print the value of the maximum integer N such that f(N)=X.


Sample Input 1

2

Sample Output 1

14
G - Magician

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

N 個のコップと 1 個の玉があります。

N 個のコップは左右に 1 列に並べられています。

コップを全てひっくり返して、 左から 1 番目のコップの中に玉を入れました。

以下のような操作を Q 回行います。

  • i 回目の操作:左から A_i 番目のコップと左から B_i 番目のコップの場所を入れ替える。このときコップの中に玉が入っていれば、玉もコップとともに場所が移る。

あなたはマジシャンなので、以下の超能力を使うことが出来ます。

  • 超能力:左から i 番目のコップに玉が入っているとき、その玉を隣のコップ(左からi-1 番目もしくは i+1 番目のコップ)の中に瞬間移動させる。ただし、左から0 番目や N+1 番目のコップは存在しないので、そこに瞬間移動させることはできない。

超能力は、すべての操作を始める前か、操作と操作の間か、すべての操作を終えた後に使うことが出来ます。

ただし、超能力を使って良いのは全体を通してたかだか 1 回までです。

全ての操作とたかだか 1 回の超能力の使用が終了したときに玉が入っている可能性があるコップの個数を求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_i < B_i \leq N

入力

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

N Q
A_1 B_1
A_2 B_2
:          
A_Q B_Q

出力

最終的に玉が入っている可能性があるコップの個数を出力せよ。


入力例 1

10 3
1 3
2 4
4 5

出力例 1

4

入力例 2

20 3
1 7
8 20
1 19

出力例 2

5

Score : 100 points

Problem Statement

You have N cups and 1 ball.

The cups are arranged in a row, from left to right.

You turned down all the cups, then inserted the ball into the leftmost cup.

Then, you will perform the following Q operations:

  • The i-th operation: swap the positions of the A_i-th and B_i-th cups from the left. If one of these cups contains the ball, the ball will also move.

Since you are a magician, you can cast a magic described below:

  • Magic: When the ball is contained in the i-th cup from the left, teleport the ball into the adjacent cup (that is, the (i-1)-th or (i+1)-th cup, if they exist).

The magic can be cast before the first operation, between two operations, or after the last operation, but you are allowed to cast it at most once during the whole process.

Find the number of cups with a possibility of containing the ball after all the operations and possibly casting the magic.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_i < B_i \leq N

Input

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

N Q
A_1 B_1
A_2 B_2
:          
A_Q B_Q

Output

Print the number of cups with a possibility of eventually containing the ball.


Sample Input 1

10 3
1 3
2 4
4 5

Sample Output 1

4

Sample Input 2

20 3
1 7
8 20
1 19

Sample Output 2

5
H - Early Bird

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

高橋君はここ数日間の自分の生活を以下のような長さ 2N の数列として記録しました。

  • a_1, b_1, a_2, b_2, ... , a_N, b_N

これは高橋君がある時刻 T から、

  • ちょうど a_1 秒間寝続ける。
  • そのあとちょうど b_1 秒間起き続ける
  • そのあとちょうど a_2 秒間寝続ける
  • (中略)
  • そのあとちょうど a_N 秒間寝続ける
  • そのあとちょうど b_N 秒間起き続ける

というような生活を送ったことを表します。

この記録の中で高橋君は N 回起床しています。

高橋君は N 回のうち、何回早起きをしたかが気になりました。

ここで「早起き」というのは午前 4 時から午前 7 時の間に起床することを指します。 起床時間がちょうど午前 4 時や、ちょうど午前 7 時でも早起きになります。

この時間帯に起床すれば早起きになるので、 同じ日のこの時間帯に 2 回以上起床したとしても 2 回以上早起きしたことになります。

しかし高橋君は時刻 T を忘れてしまいました。

N 回の起床のうち早起きだった回数として考えられる数のうちの最大値を求めてください。

なお、 1 日は 86400 秒、午前 4 時から午前 7 時の間の時間は 10800 秒です。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq a_i, b_i \leq 10^5
  • a_i, b_i はともに整数である。

入力

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

N
a_1 b_1
a_2 b_2
:
a_N b_N

出力

N 回の起床のうち早起きだった回数として考えられる数のうちの最大値を出力せよ。


入力例 1

3
28800 57600
28800 57600
57600 28800

出力例 1

2

入力例 2

10
28800 57600
4800 9600
6000 1200
600 600
300 600
5400 600
6000 5760
6760 2880
6000 12000
9000 600

出力例 2

5

Score : 100 points

Problem Statement

Takahashi recorded his daily life for the last few days as a integer sequence of length 2N, as follows:

  • a_1, b_1, a_2, b_2, ... , a_N, b_N

This means that, starting from a certain time T, he was:

  • sleeping for exactly a_1 seconds
  • then awake for exactly b_1 seconds
  • then sleeping for exactly a_2 seconds
  • :
  • then sleeping for exactly a_N seconds
  • then awake for exactly b_N seconds

In this record, he waked up N times.

Takahashi is wondering how many times he waked up early during the recorded period.

Here, he is said to wake up early if he wakes up between 4:00 AM and 7:00 AM, inclusive.

If he wakes up more than once during this period, each of these awakenings is counted as waking up early.

Unfortunately, he forgot the time T.

Find the maximum possible number of times he waked up early during the recorded period.

For your information, a day consists of 86400 seconds, and the length of the period between 4:00 AM and 7:00 AM is 10800 seconds.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq a_i, b_i \leq 10^5
  • a_i and b_i are integers.

Input

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

N
a_1 b_1
a_2 b_2
:
a_N b_N

Output

Print the maximum possible number of times he waked up early during the recorded period.


Sample Input 1

3
28800 57600
28800 57600
57600 28800

Sample Output 1

2

Sample Input 2

10
28800 57600
4800 9600
6000 1200
600 600
300 600
5400 600
6000 5760
6760 2880
6000 12000
9000 600

Sample Output 2

5
I - 3y3s Challenge

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

1 から N までの整数が振られた N 人の人がいます。これから N-1 秒間、「目があったら負けチャレンジ」をしてもらいます。

それぞれの人は、チャレンジが開始してから N-1 秒間で、自分以外の N-1 人を 1 人あたりちょうど 1 秒ずつ、なんらかの順番で見つめます。

このとき互いに見つめ合っている 2 人ができたら、チャレンジは失敗です。

チャレンジが成功するためには各人がどのような順番で他の N-1 人を見つめればよいかを求めてください。

制約

  • 2 \leq N \leq 100

入力

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

N

出力

もし、チャレンジが成功するような見つめ方がない場合は、 -1 を出力せよ。

チャレンジが成功するような見つめ方がある場合には、そのうち好きなもの 1 つを、以下の形式で出力せよ。

A_{1,1} A_{1,2} ... A_{1, N-1}
A_{2,1} A_{2,2} ... A_{2, N-1}
:
A_{N,1} A_{N,2} ... A_{N, N-1}

ここで A_{i, j}i 番目の人が j 番目に見つめる人の番号である。

判定

以下の全ての条件を満たしているときのみ、その出力は正解とみなされる。

  • 1 \leq A_{i,j} \leq N
  • 全ての i について A_{i,1}, A_{i,2}, ... , A_{i, N-1} は値が異なる。
  • X = A_{i, j} として A_{X, j} \neq i が常に成り立つ。

入力例 1

7

出力例 1

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

入力例 2

2

出力例 2

-1

Score : 100 points

Problem Statement

There are N persons, conveniently numbered 1 through N. They will take 3y3s Challenge for N-1 seconds.

During the challenge, each person must look at each of the N-1 other persons for 1 seconds, in some order.

If any two persons look at each other during the challenge, the challenge ends in failure.

Find the order in which each person looks at each of the N-1 other persons, to be successful in the challenge.

Constraints

  • 2 \leq N \leq 100

Input

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

N

Output

If there exists no way to be successful in the challenge, print -1.

If there exists a way to be successful in the challenge, print any such way in the following format:

A_{1,1} A_{1,2} ... A_{1, N-1}
A_{2,1} A_{2,2} ... A_{2, N-1}
:
A_{N,1} A_{N,2} ... A_{N, N-1}

where A_{i, j} is the index of the person that the person numbered i looks at during the j-th second.

Judging

The output is considered correct only if all of the following conditions are satisfied:

  • 1 \leq A_{i,j} \leq N
  • For each i, A_{i,1}, A_{i,2}, ... , A_{i, N-1} are pairwise distinct.
  • Let X = A_{i, j}, then A_{X, j} \neq i always holds.

Sample Input 1

7

Sample Output 1

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

Sample Input 2

2

Sample Output 2

-1
J - Connected Checkerboard

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

N マス × N マスのチェスボードがあります。

最も左上のマスから、右に i マス、下に j マス進んだマスを (i, j) と呼びます。特に、最も左上のマスは (0, 0) です。

i+j が偶数であるようなマス (i, j) は黒、それ以外のマスは白で塗られています。

これから、いくつかの白いマスを黒に塗り替えることで以下の条件が満たされるようにします。

  • マス (0, 0) から始めて、辺を共有する黒いマスに移動するという操作を繰り返したときに、全ての黒いマスにたどり着くことができる。

170000 個以下のマスを黒く塗り替えることで条件を達成してください。

制約

  • 1 \leq N \leq 1,000

入力

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

N

出力

条件を達成するために黒く塗り替えたマスの情報を以下の形式で出力せよ。

K
x_1 y_1
x_2 y_2
:
x_K y_K

これは、全部で K 個のマスを黒く塗り替えており、i 番目に (x_i, y_i) のマスを黒く塗り替えたことを表す。

判定

以下の全ての条件を満たしているときのみ、その出力は正解とみなされる。

  • 0 \leq K \leq 170000
  • 0 \leq x_i, y_i \leq N-1
  • 全ての i に対して x_i + y_i は奇数。
  • i \neq j ならば (x_i, y_i) \neq (x_j, y_j)
  • 出力された全てのマスを黒く塗ることで問題文中の条件が達成されている。

入力例 1

2

出力例 1

1
1 0

入力例 2

4

出力例 2

3
0 1
2 1
2 3

Score : 100 points

Problem Statement

We have an N×N checkerboard.

From the square at the upper left corner, a square that is i squares to the right and j squares below is denoted as (i, j). Particularly, the square at the upper left corner is denoted as (0, 0).

Each square (i, j) such that i+j is even, is colored black, and the other squares are colored white.

We will satisfy the following condition by painting some of the white squares:

  • Any black square can be reached from square (0, 0) by repeatedly moving to a black square that shares a side with the current square.

Achieve the objective by painting at most 170000 squares black.

Constraints

  • 1 \leq N \leq 1,000

Input

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

N

Output

Print the squares to paint in the following format:

K
x_1 y_1
x_2 y_2
:
x_K y_K

This means that a total of K squares are painted black, the i-th of which is (x_i, y_i).

Judging

The output is considered correct only if all of the following conditions are satisfied:

  • 0 \leq K \leq 170000
  • 0 \leq x_i, y_i \leq N-1
  • For each i, x_i + y_i is odd.
  • If i \neq j, then (x_i, y_i) \neq (x_j, y_j).
  • The condition in the statement is satisfied by painting all specified squares.

Sample Input 1

2

Sample Output 1

1
1 0

Sample Input 2

4

Sample Output 2

3
0 1
2 1
2 3
K - Problem on Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

頂点数 N の木があります。木の頂点にはそれぞれ 1 から N までの番号が振られています。

辺は N-1 本あり、 i 番目の辺は頂点 p_i と頂点 q_i を結んでいます。

相異なる頂点の列 v_1, v_2, ..., v_M であって、次の条件を満たすもののうち、 M が最大となるものの M を求めてください。

  • 全ての 1 \leq i < M について、v_iv_{i+1} を結ぶ経路の上に、v の他の頂点が存在しない。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq p_i, q_i \leq N
  • 与えられるグラフは木である。

入力

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

N
p_1 q_1
p_2 q_2
:
p_{N-1} q_{N-1}

出力

条件を満たす相異なる頂点の列のうち要素数 M が最大となるものの M を出力せよ。


入力例 1

4
1 2
2 3
2 4

出力例 1

3

入力例 2

10
7 9
1 2
6 4
8 1
3 7
6 5
2 10
9 6
2 6

出力例 2

8

Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.

The i-th of the N-1 edges connects the vertices p_i and q_i.

Among the sequences of distinct vertices v_1, v_2, ..., v_M that satisfy the following condition, find the maximum value of M.

  • For every 1 \leq i < M, the path connecting the vertices v_i and v_{i+1} do not contain any vertex in v, except for v_i and v_{i+1}.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq p_i, q_i \leq N
  • The given graph is a tree.

Input

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

N
p_1 q_1
p_2 q_2
:
p_{N-1} q_{N-1}

Output

Print the maximum value of M, the number of elements, among the sequences of vertices that satisfy the condition.


Sample Input 1

4
1 2
2 3
2 4

Sample Output 1

3

Sample Input 2

10
7 9
1 2
6 4
8 1
3 7
6 5
2 10
9 6
2 6

Sample Output 2

8