A - Swap Odd and Even

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字からなる長さが偶数の文字列 S が与えられます。S の長さを |S|Si 文字目を S_i で表します。

i = 1, 2, \ldots, \frac{|S|}{2} の順に以下の操作を行い、すべての操作を終えた後の S を出力してください。

  • S_{2i-1}S_{2i} を入れ替える

制約

  • S は英小文字からなる長さが偶数の文字列
  • S の長さは 100 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

abcdef

出力例 1

badcfe

操作を行う前は S = abcdef です。
i = 1 について操作を行うと、S_1S_2 が入れ替わるので S = bacdef になります。
i = 2 について操作を行うと、S_3S_4 が入れ替わるので S = badcef になります。
i = 3 について操作を行うと、S_5S_6 が入れ替わるので S = badcfe になります。
したがって、badcfe を出力します。


入力例 2

aaaa

出力例 2

aaaa

入力例 3

atcoderbeginnercontest

出力例 3

taocedbrgeniencrnoetts

Score : 100 points

Problem Statement

You are given a string S of even length consisting of lowercase English letters. Let |S| be the length of S, and S_i be the i-th character of S.

Perform the following operation for each i = 1, 2, \ldots, \frac{|S|}{2} in this order, and print the final S.

  • Swap S_{2i-1} and S_{2i}.

Constraints

  • S is a string of even length consisting of lowercase English letters.
  • The length of S is at most 100.

Input

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

S

Output

Print the answer.


Sample Input 1

abcdef

Sample Output 1

badcfe

Initially, S = abcdef.
Performing the operation for i = 1 swaps S_1 and S_2, making S = bacdef.
Performing the operation for i = 2 swaps S_3 and S_4, making S = badcef.
Performing the operation for i = 3 swaps S_5 and S_6, making S = badcfe.
Thus, badcfe should be printed.


Sample Input 2

aaaa

Sample Output 2

aaaa

Sample Input 3

atcoderbeginnercontest

Sample Output 3

taocedbrgeniencrnoetts
B - Call the ID Number

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1 、人 2\ldots 、人 N番号をつけられた N 人の人がいます。

N 人は、人 1 、人 2\ldots 、人 N の順番に下記の行動をちょうど 1 回ずつ行います。

  • i 自身がまだ一度も番号を呼ばれていないなら、人 A_i の番号を呼ぶ。

最後まで番号を一度も呼ばれない人全員の番号を昇順に列挙してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • A_i \neq i
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

下記の形式にしたがって、最後まで番号を一度も呼ばれない人全員の番号を昇順に列挙せよ。

K
X_1 X_2 \ldots X_K

すなわち、まず 1 行目に、最後まで番号を一度も呼ばれない人の人数 K を出力し、 2 行目に、最後まで番号を一度も呼ばれない人全員の番号を昇順に並べた列 (X_1, X_2, \ldots, X_K) を空白区切りで出力せよ。


入力例 1

5
3 1 4 5 4

出力例 1

2
2 4

5 人の行動は下記の通りです。

  • 1 はまだ番号を一度も呼ばれていないので、人 1 は人 3 の番号を呼びます。
  • 2 はまだ番号を一度も呼ばれていないので、人 2 は人 1 の番号を呼びます。
  • 3 はすでに人 1 によって番号を呼ばれているので、何もしません。
  • 4 はまだ番号を一度も呼ばれていないので、人 4 は人 5 の番号を呼びます。
  • 5 はすでに人 4 によって番号を呼ばれているので、何もしません。

よって、最後まで番号を一度も呼ばれないのは人 2 と人 4 です。


入力例 2

20
9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 12 13 2 12

出力例 2

10
1 2 5 6 8 11 14 17 18 20

Score : 200 points

Problem Statement

There are N people whose IDs are 1, 2, \ldots, and N.

Each of person 1, person 2, \ldots, and person N performs the following action once in this order:

  • If person i's ID has not been called out yet, call out person A_i's ID.

Enumerate the IDs of all the people whose IDs are never called out until the end in ascending order.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • A_i \neq i
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Enumerate the IDs of all the people whose IDs are not called out until the end in ascending order in the following format:

K
X_1 X_2 \ldots X_K

In other words, the first line should contain the number of people, K, whose IDs are never called out until the end; the second line should contain the sequence (X_1, X_2, \ldots, X_K) of IDs of such people in ascending order, with spaces in between.


Sample Input 1

5
3 1 4 5 4

Sample Output 1

2
2 4

The five people's actions are as follows.

  • Person 1's ID has not been called out yet, so person 1 calls out person 3's ID.
  • Person 2's ID has not been called out yet, so person 2 calls out person 1's ID.
  • Person 3's ID has already been called out by person 1, so nothing happens.
  • Person 4's ID has not been called out yet, so person 4 calls out person 5's ID.
  • Person 5's ID has already been called out by person 4, so nothing happens.

Therefore, person 2 and 4's IDs are not called out until the end.


Sample Input 2

20
9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 12 13 2 12

Sample Output 2

10
1 2 5 6 8 11 14 17 18 20
C - Make Takahashi Happy

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

HW 列のマス目があります。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす 2 つの整数 i, j について、 上から i 行目、左から j 列目のマス(以下、(i, j) と表す)には、整数 A_{i, j} が書かれています。

いま、高橋君は (1, 1) にいます。 これから高橋君は「いまいるマスから右または下に隣接するマスに移動する」ことを繰り返して、(H, W) まで移動します。 ただし、その過程でマス目の外部に移動することは出来ません。

その結果、高橋君が通ったマス(始点 (1, 1) と終点 (H, W) を含む)に書かれた整数がすべて異なるとき、高橋君は嬉しくなります。 高橋君の移動経路として考えられるもののうち、高橋君が嬉しくなるものの個数を出力してください。

制約

  • 2 \leq H, W \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • 入力はすべて整数

入力

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

H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}

出力

答えを出力せよ。


入力例 1

3 3
3 2 2
2 1 3
1 5 4

出力例 1

3

高橋君の移動経路として考えられるものは下記の 6 通りです。

  • (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 2, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります
  • (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります

よって、高橋君が嬉しくなる移動経路は、上で 3, 5, 6 番目に述べた 3 個です。


入力例 2

10 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

出力例 2

48620

この例では、高橋君は考えられるどの経路を通っても嬉しくなります。

Score : 300 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. For two integers i and j such that 1 \leq i \leq H and 1 \leq j \leq W, the square at the i-th row from the top and j-th column from the left (which we denote by (i, j)) has an integer A_{i, j} written on it.

Takahashi is currently at (1,1). From now on, he repeats moving to an adjacent square to the right of or below his current square until he reaches (H, W). When he makes a move, he is not allowed to go outside the grid.

Takahashi will be happy if the integers written on the squares he visits (including initial (1, 1) and final (H, W)) are distinct. Find the number of his possible paths that make him happy.

Constraints

  • 2 \leq H, W \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • All values in the input are integers.

Input

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

H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}

Output

Print the answer.


Sample Input 1

3 3
3 2 2
2 1 3
1 5 4

Sample Output 1

3

There are six possible paths:

  • (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 2, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.

Thus, the third, fifth, and sixth paths described above make him happy.


Sample Input 2

10 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Sample Output 2

48620

In this example, every possible path makes him happy.

D - Tying Rope

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

一方の端が赤に塗られており、もう一方の端が青に塗られているロープが N 本あります。ロープには 1 から N までの番号がつけられています。

これからロープの端を結ぶ操作を M 回行います。i 回目の操作ではロープ A_i の色 B_i の端とロープ C_i の色 D_i の端を結びます。ただし、色 R は赤を意味し、色 B は青を意味します。各ロープについて、同じ色の端が複数回結ばれることはありません。

すべての操作を終えた後に、ひとつながりになっているロープの組について環状になっているものとそうでないものの個数を出力してください。

ただし、ひとつながりになっているロープの組 \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace が環状になっているとは、v の要素の順序を適切に入れ替えることで各 0 \leq i < x についてロープ v_i とロープ v_{(i+1) \bmod x} が結ばれているようにできることをいいます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq N
  • (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
  • (A_i, B_i) \neq (C_j, D_j)
  • N, M, A_i, C_i は整数
  • B_i, D_iRB のいずれか

入力

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

N M
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_M B_M C_M D_M

出力

ひとつながりになっているロープの組について、環状になっているものの個数を X、そうでないものの個数を Y として XY をこの順に空白区切りで出力せよ。


入力例 1

5 3
3 R 5 B
5 R 3 B
4 R 2 B

出力例 1

1 2

ひとつながりになっているロープの組は \lbrace 1 \rbrace\lbrace 2,4 \rbrace\lbrace 3,5 \rbrace3 つです。

ロープ \lbrace 3,5 \rbrace の組は環状になっており、ロープ \lbrace 1 \rbrace\lbrace 2,4 \rbrace の組は環状になっていません。したがって、X = 1, Y = 2 です。


入力例 2

7 0

出力例 2

0 7

入力例 3

7 6
5 R 3 R
7 R 4 R
4 B 1 R
2 R 3 B
2 B 5 B
1 B 7 B

出力例 3

2 1

Score : 400 points

Problem Statement

There are N ropes numbered 1 through N. One end of each rope is painted red, and the other is painted blue.

You are going to perform M operations of tying ropes. In the i-th operation, you tie the end of rope A_i painted B_i with the end of rope C_i painted D_i, where R means red and B means blue. For each rope, an end with the same color is not tied multiple times.

Find the number of groups of connected ropes that form cycles, and the number of those that do not, after all the operations.

Here, a group of connected ropes \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace is said to form a cycle if one can rearrange the elements of v so that, for each 0 \leq i < x, rope v_i is tied to rope v_{(i+1) \bmod x}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq N
  • (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
  • (A_i, B_i) \neq (C_j, D_j)
  • N, M, A_i, and C_i are integers.
  • B_i is R or B, and so is D_i.

Input

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

N M
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_M B_M C_M D_M

Output

Print X and Y in this order, separated by a space, where X is the number of groups of connected ropes that form cycles, and Y is the number of those that do not.


Sample Input 1

5 3
3 R 5 B
5 R 3 B
4 R 2 B

Sample Output 1

1 2

There are three groups of connected ropes: \lbrace 1 \rbrace, \lbrace 2,4 \rbrace, and \lbrace 3,5 \rbrace.

The group of ropes \lbrace 3,5 \rbrace forms a cycle, while the groups of rope \lbrace 1 \rbrace and ropes \lbrace 2,4 \rbrace do not. Thus, X = 1 and Y = 2.


Sample Input 2

7 0

Sample Output 2

0 7

Sample Input 3

7 6
5 R 3 R
7 R 4 R
4 B 1 R
2 R 3 B
2 B 5 B
1 B 7 B

Sample Output 3

2 1
E - Geometric Progression

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

整数 A, X, M が与えられます。\displaystyle \sum_{i = 0}^{X-1} A^iM で割った余りを求めてください。

制約

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • 入力はすべて整数

入力

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

A X M

出力

答えを出力せよ。


入力例 1

3 4 7

出力例 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40 です。407 で割った余りは 5 であるため、5 を出力します。


入力例 2

8 10 9

出力例 2

0

入力例 3

1000000000 1000000000000 998244353

出力例 3

919667211

Score : 500 points

Problem Statement

Given integers A, X, and M, find \displaystyle \sum_{i = 0}^{X-1} A^i, modulo M.

Constraints

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • All values in the input are integers.

Input

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

A X M

Output

Print the answer.


Sample Input 1

3 4 7

Sample Output 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40, which equals 5 modulo 7, so 5 should be printed.


Sample Input 2

8 10 9

Sample Output 2

0

Sample Input 3

1000000000 1000000000000 998244353

Sample Output 3

919667211
F - Zero or One

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

2 以上の整数 N が与えられます。下記の条件を満たす 2 以上の整数 b の個数を出力してください。

  • Nb 進法で表記したとき、すべての桁について「その桁が 0 または 1 である」が成り立つ。

T 個の独立なテストケースについて答えを求めてください。

なお、本問題の制約下において、上記の「条件を満たす 2 以上の整数 b の個数」は有限であることが証明できます。

制約

  • 1 \leq T \leq 1000
  • 2 \leq N \leq 10^{18}
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_ii 番目のテストケースを表す。

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

各テストケースは以下の形式で与えられる。

N

出力

T 行出力せよ。 i = 1, 2, \ldots, T について、i 行目には i 番目のテストケースに対する答えを出力せよ。


入力例 1

3
12
2
36

出力例 1

4
1
5

1 番目のテストケースについて、問題文中の条件を満たす b は、b = 2, 3, 11, 124 つです。 実際、N = 122, 3, 11, 12 進法で表すと、それぞれ 1100, 110, 11, 10 となります。

Score : 500 points

Problem Statement

Given an integer N not less than 2, find the number of integers b not less than 2 such that:

  • when N is written in base b, every digit is 0 or 1.

Find the answer for T independent test cases.

It can be proved that there is a finite number of desired integers b not less than 2 under the constraints of this problem.

Constraints

  • 1 \leq T \leq 1000
  • 2 \leq N \leq 10^{18}
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N

Output

Print T lines. For i = 1, 2, \ldots, T, the i-th line should contain the answer to the i-th test case.


Sample Input 1

3
12
2
36

Sample Output 1

4
1
5

For the first test case, four b's satisfy the condition in the problem statement: b = 2, 3, 11, 12. Indeed, when N=12 is written in base 2, 3, 11, and 12, it becomes 1100, 110, 11 and 10, respectively.

G - Triple Index

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の正整数列 (A_1, A_2, \ldots, A_N) と、この数列に関する Q 個のクエリが与えられます。

q = 1, 2, \ldots, Q のそれぞれについて、q 番目のクエリでは整数の 2 つ組 (l_q, r_q) が与えられるので、
下記の 2 つの条件をともに満たす整数の 3 つ組 (i, j, k) の個数を出力してください。

  • l_q \leq i \lt j \lt k \leq r_q
  • A_i = A_j = A_k

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • 1 \leq l_q \leq r_q \leq N
  • 入力はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

出力

Q 行出力せよ。 q = 1, 2, \ldots, Q について、q 行目には q 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

5
2
4
0

1 番目のクエリについて、問題文中の 2 つの条件をともに満たす整数の 3 つ組 (i, j, k) は、 (1, 5, 9), (4, 6, 8), (4, 6, 10), (4, 8, 10), (6, 8, 10)5 個です。


入力例 2

20 10
2 2 2 2 1 1 2 2 1 1 1 2 1 2 1 2 2 1 2 1
12 16
17 18
12 18
4 9
13 13
2 5
6 13
2 14
7 14
8 12

出力例 2

1
0
5
2
0
1
11
55
8
1

Score : 600 points

Problem Statement

You are given a length-N sequence (A_1, A_2, \ldots, A_N) of positive integers, and Q queries about the sequence.

For each q = 1, 2, \ldots, Q, the q-th query gives you an integer pair (l_q, r_q);
print the number of integer triplets (i, j, k) such that

  • l_q \leq i \lt j \lt k \leq r_q, and
  • A_i = A_j = A_k.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • 1 \leq l_q \leq r_q \leq N
  • All values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

Output

Print Q lines. For q = 1, 2, \ldots, Q, the q-th line should contain the answer to the q-th query.


Sample Input 1

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

Sample Output 1

5
2
4
0

For the first query, there are five triplets of integers that satisfy the conditions in the problem statement: (1, 5, 9), (4, 6, 8), (4, 6, 10), (4, 8, 10), and (6, 8, 10).


Sample Input 2

20 10
2 2 2 2 1 1 2 2 1 1 1 2 1 2 1 2 2 1 2 1
12 16
17 18
12 18
4 9
13 13
2 5
6 13
2 14
7 14
8 12

Sample Output 2

1
0
5
2
0
1
11
55
8
1
Ex - Optimal Path Decomposition

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点の木が与えられます。頂点には 1 から N までの番号がついており、i 番目の辺は頂点 A_i と頂点 B_i を結んでいます。

各頂点に以下の条件を満たすように色を塗ることができる整数 K の最小値を求めてください。ただし、使える色の種類数に制限はありません。

  • 各色について、その色で塗られた頂点の集合は連結で単純パスをなす
  • 任意の木上の単純パスについて、そのパス内に含まれる頂点に塗られた色の種類数は K 以下

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • 与えられるグラフは木
  • 入力はすべて整数

入力

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

K = 3 のとき、頂点 1,2,3,4,5 を色 1、頂点 6 を色 2、頂点 7 を色 3 で塗るなどの方法で条件を満たすことができます。 K \leq 2 とすると条件を満たす色の塗り方は存在しないので答えは 3 です。


入力例 2

6
3 5
6 4
6 3
4 2
1 5

出力例 2

1

入力例 3

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

出力例 3

3

Score : 600 points

Problem Statement

You are given a tree with N vertices numbered 1 through N. The i-th edge connects vertex A_i and vertex B_i.

Find the minimum integer K such that you can paint each vertex in a color so that the following two conditions are satisfied. You may use any number of colors.

  • For each color, the set of vertices painted in the color is connected and forms a simple path.
  • For all simple paths on the tree, there are at most K distinct colors of the vertices in the path.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • The given graph is a tree.
  • All values in the input are integers.

Input

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

3

If K=3, the conditions can be satisfied by painting vertices 1,2,3,4, and 5 in color 1, vertex 6 in color 2, and vertex 7 in color 3. If K \leq 2, there is no way to paint the vertices to satisfy the conditions, so the answer is 3.


Sample Input 2

6
3 5
6 4
6 3
4 2
1 5

Sample Output 2

1

Sample Input 3

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

Sample Output 3

3