実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
キーエンスでは、役割や年齢、立場の違いに関係なく「さん」付けして呼ぶという文化があります。 新入社員が社長を呼ぶときも「中田さん」と呼びます。
ある人の苗字と名前がそれぞれ文字列 S,T として与えられます。
苗字、スペース( )、敬称(san)をこの順に連結した文字列を出力してください。
制約
- S,T は以下の各条件を満たす文字列
- 長さ 1 以上 10 以下
- 先頭の文字は英大文字
- 先頭以外の文字は英小文字
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
苗字、スペース( )、敬称(san)をこの順に連結した文字列を出力せよ。
入力例 1
Takahashi Chokudai
出力例 1
Takahashi san
苗字(Takahashi)、スペース( )、敬称(san)をこの順に連結した文字列を出力します。
入力例 2
K Eyence
出力例 2
K san
Score : 100 points
Problem Statement
Keyence has a culture of addressing everyone with the honorific "san," regardless of their role, age, or position. Even a new employee would call the president "Nakata-san." [Translator's note: this is a bit unusual in Japan.]
You are given a person's surname and first name as strings S and T, respectively.
Print the concatenation of the surname, a space ( ), and the honorific (san) in this order.
Constraints
- Each of S and T is a string that satisfies the following conditions.
- The length is between 1 and 10, inclusive.
- The first character is an uppercase English letter.
- All characters except the first one are lowercase English letters.
Input
The input is given from Standard Input in the following format:
S T
Output
Print the concatenation of the surname, a space ( ), and the honorific (san) in this order.
Sample Input 1
Takahashi Chokudai
Sample Output 1
Takahashi san
Print the concatenation of the surname (Takahashi), a space ( ), and the honorific (san) in this order.
Sample Input 2
K Eyence
Sample Output 2
K san
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
正整数 N が与えられます。
1 以上 N 以下の整数をすべて足し合わせた値 1+2+\cdots+N を出力してください。
制約
- 1 \leq N \leq 100
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
1 以上 N 以下の整数をすべて足し合わせた値を出力せよ。
入力例 1
5
出力例 1
15
1+2+3+4+5=15 のため、15 を出力します。
入力例 2
1
出力例 2
1
入力例 3
29
出力例 3
435
Score : 100 points
Problem Statement
You are given a positive integer N.
Output the sum of all integers from 1 to N, that is, 1+2+\cdots+N.
Constraints
- 1 \leq N \leq 100
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Output the sum of all integers from 1 to N.
Sample Input 1
5
Sample Output 1
15
Since 1+2+3+4+5=15, output 15.
Sample Input 2
1
Sample Output 2
1
Sample Input 3
29
Sample Output 3
435
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
2N 人の人が横一列に並んでおり、左から i 番目の人は色 A_i の服を着ています。ここで、服の色は 1 から N の N 色であり、それぞれの色についてちょうど 2 人の人がその色の服を着ています。
i=1,2,\ldots,N のうち、以下の条件を満たすものは何通りあるか求めてください。
- 色 i の服を着た二人の人の間にはちょうど一人いる。
制約
- 2\leq N\leq 100
- 1\leq A_i \leq N
- A には 1 以上 N 以下の整数全てがそれぞれ 2 個ずつ含まれる
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{2N}
出力
答えを出力せよ。
入力例 1
3 1 2 1 3 2 3
出力例 1
2
条件を満たす i は 1 と 3 の 2 個です。
実際、色 1 の服を着ているのは左から 1 番目の人と左から 3 番目の人で、間にちょうど一人います。
入力例 2
2 1 1 2 2
出力例 2
0
条件を満たす i が存在しない場合もあります。
入力例 3
4 4 3 2 3 2 1 4 1
出力例 3
3
Score : 150 points
Problem Statement
There are 2N people standing in a row, and the person at the i-th position from the left is wearing clothes of color A_i. Here, the clothes have N colors from 1 to N, and exactly two people are wearing clothes of each color.
Find how many of the integers i=1,2,\ldots,N satisfy the following condition:
- There is exactly one person between the two people wearing clothes of color i.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq N
- Each integer from 1 through N appears exactly twice in A.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_{2N}
Output
Print the answer.
Sample Input 1
3 1 2 1 3 2 3
Sample Output 1
2
There are two values of i that satisfy the condition: 1 and 3.
In fact, the people wearing clothes of color 1 are at the 1st and 3rd positions from the left, with exactly one person in between.
Sample Input 2
2 1 1 2 2
Sample Output 2
0
There may be no i that satisfies the condition.
Sample Input 3
4 4 3 2 3 2 1 4 1
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 種類の元素があり、元素には 1, 2, \ldots, N の番号が付けられています。
元素どうしは合成させることができ、元素 i と元素 j を合成すると i \geq j のとき元素 A_{i, j} に、i < j のとき元素 A_{j, i} に変化します。
元素 1 に対して元素 1, 2, \ldots, N をこの順に合成したとき、最終的に得られる元素を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq A_{i, j} \leq N
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1, 1}
A_{2, 1} A_{2, 2}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}
出力
最終的に得られる元素の番号を出力せよ。
入力例 1
4 3 2 4 3 1 2 2 1 2 4
出力例 1
2
-
元素 1 と元素 1 を合成すると、元素 3 が得られます。
-
元素 3 と元素 2 を合成すると、元素 1 が得られます。
-
元素 1 と元素 3 を合成すると、元素 3 が得られます。
-
元素 3 と元素 4 を合成すると、元素 2 が得られます。
したがって、出力するべき値は 2 です。
入力例 2
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
出力例 2
5
入力例 3
6 2 1 5 1 6 3 2 6 1 4 2 1 1 1 6 5 6 1 2 2 5
出力例 3
5
Score : 200 points
Problem Statement
There are N types of elements numbered 1, 2, \ldots, N.
Elements can be combined with each other. When elements i and j are combined, they transform into element A_{i, j} if i \geq j, and into element A_{j, i} if i < j.
Starting with element 1, combine it with elements 1, 2, \ldots, N in this order. Find the final element obtained.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_{i, j} \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_{1, 1}
A_{2, 1} A_{2, 2}
\vdots
A_{N, 1} A_{N, 2} \ldots A_{N, N}
Output
Print the number representing the final element obtained.
Sample Input 1
4 3 2 4 3 1 2 2 1 2 4
Sample Output 1
2
-
Combining element 1 with element 1 results in element 3.
-
Combining element 3 with element 2 results in element 1.
-
Combining element 1 with element 3 results in element 3.
-
Combining element 3 with element 4 results in element 2.
Therefore, the value to be printed is 2.
Sample Input 2
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Sample Output 2
5
Sample Input 3
6 2 1 5 1 6 3 2 6 1 4 2 1 1 1 6 5 6 1 2 2 5
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
黒板に整数 N が 1 個書かれています。
高橋君は黒板に書かれている 2 以上の整数が全て無くなるまで以下の一連の操作を繰り返します。
- 黒板に書かれている 2 以上の整数を 1 つ選び x とする。
- 黒板から x を 1 個消す。そして、2 個の整数 \left \lfloor \dfrac{x}{2} \right\rfloor, \left\lceil \dfrac{x}{2} \right\rceil を新たに黒板に書く。
- この一連の操作を行うために高橋君は x 円払う必要がある。
ここで \lfloor a \rfloor は a 以下の整数のうち最大のものを、\lceil a \rceil は a 以上の整数のうち最小のものを意味します。
操作を行えなくなった時点で高橋君が払った金額の総和は何円ですか?
なお、どのような順番で操作を行っても高橋君が払う金額の総和は一定であることが証明できます。
制約
- 2 \leq N \leq 10^{17}
入力
入力は以下の形式で標準入力から与えられる。
N
出力
高橋君が払った金額の総和が何円であるかを出力せよ。
入力例 1
3
出力例 1
5
高橋君が行う操作の一例を挙げると次のようになります。
- はじめ、黒板には 3 が 1 個書かれている。
- 高橋君は 3 を選ぶ。高橋君は 3 円を払い、黒板から 3 を 1 個消して \left \lfloor \dfrac{3}{2} \right\rfloor = 1, \left\lceil \dfrac{3}{2} \right\rceil = 2 を新たに黒板に書く。
- 黒板には 2 が 1 個と 1 が 1 個書かれている。
- 高橋君は 2 を選ぶ。高橋君は 2 円を払い、黒板から 2 を 1 個消して \left \lfloor \dfrac{2}{2} \right\rfloor = 1, \left\lceil \dfrac{2}{2} \right\rceil = 1 を新たに黒板に書く。
- 黒板には 1 が 3 個書かれている。
- 黒板から 2 以上の整数が全て無くなったので操作を終了する。
操作全体で高橋君は 3 + 2 = 5 円払ったので、5 を出力します。
入力例 2
340
出力例 2
2888
入力例 3
100000000000000000
出力例 3
5655884811924144128
Score: 300 points
Problem Statement
There is a single integer N written on a blackboard.
Takahashi will repeat the following series of operations until all integers not less than 2 are removed from the blackboard:
- Choose one integer x not less than 2 written on the blackboard.
- Erase one occurrence of x from the blackboard. Then, write two new integers \left \lfloor \dfrac{x}{2} \right\rfloor and \left\lceil \dfrac{x}{2} \right\rceil on the blackboard.
- Takahashi must pay x yen to perform this series of operations.
Here, \lfloor a \rfloor denotes the largest integer not greater than a, and \lceil a \rceil denotes the smallest integer not less than a.
What is the total amount of money Takahashi will have paid when no more operations can be performed?
It can be proved that the total amount he will pay is constant regardless of the order in which the operations are performed.
Constraints
- 2 \leq N \leq 10^{17}
Input
The input is given from Standard Input in the following format:
N
Output
Print the total amount of money Takahashi will have paid, in yen.
Sample Input 1
3
Sample Output 1
5
Here is an example of how Takahashi performs the operations:
- Initially, there is one 3 written on the blackboard.
- He chooses 3. He pays 3 yen, erases one 3 from the blackboard, and writes \left \lfloor \dfrac{3}{2} \right\rfloor = 1 and \left\lceil \dfrac{3}{2} \right\rceil = 2 on the blackboard.
- There is one 2 and one 1 written on the blackboard.
- He chooses 2. He pays 2 yen, erases one 2 from the blackboard, and writes \left \lfloor \dfrac{2}{2} \right\rfloor = 1 and \left\lceil \dfrac{2}{2} \right\rceil = 1 on the blackboard.
- There are three 1s written on the blackboard.
- Since all integers not less than 2 have been removed from the blackboard, the process is finished.
Takahashi has paid a total of 3 + 2 = 5 yen for the entire process, so print 5.
Sample Input 2
340
Sample Output 2
2888
Sample Input 3
100000000000000000
Sample Output 3
5655884811924144128
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は、レジ打ちの仕事をしています。
レジの機械には 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 の 11 個のボタンがあります。
レジの機械には、はじめ 0 が表示されています。
ボタン 00 を押すと、表示されている数が 100 倍されます。
それ以外のボタンを押すと、表示されている数が 10 倍されたあとに、押されたボタンに書かれている数が加算されます。
高橋君は、レジに整数 S を表示させたいです。 レジに S が表示されている状態にするためには、少なくとも何回ボタンを押す必要があるか求めてください。
制約
- 1\leq S\leq 10^{100000}
- S は整数
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを 1 行で出力せよ。
入力例 1
40004
出力例 1
4
例えば、次のように操作することでボタンを 4 回押して 40004 を表示させることができます。 はじめ、レジには 0 が表示されています。
- ボタン
4を押す。レジに表示されている数は 4 となる。 - ボタン
00を押す。レジに表示されている数は 400 となる。 - ボタン
0を押す。レジに表示されている数は 4000 となる。 - ボタン
4を押す。レジに表示されている数は 40004 となる。
3 回までボタンを押すことでレジに 40004 を表示させることはできないので、出力すべき値は 4 です。
入力例 2
1355506027
出力例 2
10
入力例 3
10888869450418352160768000001
出力例 3
27
S は 64\operatorname{bit} 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
Takahashi is a cashier.
There is a cash register with 11 keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
The cash register initially displays 0.
Whenever he types the key 00, the displayed number is multiplied by 100;
whenever he types one of the others, the displayed number is multiplied by 10, and then added by the number written on the key.
Takahashi wants the cash register to display an integer S. At least how many keystrokes are required to make it display S?
Constraints
- 1\leq S\leq 10^{100000}
- S is an integer.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer in a line.
Sample Input 1
40004
Sample Output 1
4
For example, the following four keystrokes make the cash register display 40004. Initially, the cash register displays 0.
- Type the key
4. It now displays 4. - Type the key
00. It now displays 400. - Type the key
0. It now displays 4000. - Type the key
4. It now displays 40004.
He cannot make it display 40004 with three or fewer keystrokes, so 4 should be printed.
Sample Input 2
1355506027
Sample Output 2
10
Sample Input 3
10888869450418352160768000001
Sample Output 3
27
Note that S may not fit into a 64-\operatorname{bit} integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
あなたは最初、空文字列 S を持っています。
さらに、文字列がいくつか入った袋 1,2,\dots,N があります。
袋 i には A_i 個の文字列 S_{i,1},S_{i,2},\dots,S_{i,A_i} が入っています。
これから、以下の手順を i=1,2,\dots,N について繰り返します。
- 以下のふたつの行動のうち、どちらかを選択して行う。
- 1 円を支払い、袋 i からちょうどひとつの文字列を選択して S の末尾に連結する。
- 何もしない。
文字列 T が与えられるとき、最終的に S と T を一致させるために必要な最小の金額を求めてください。
但し、どのようにしても最終的な S を T に一致させることができない場合、 -1 と出力してください。
制約
- T は長さ 1 以上 100 以下の英小文字からなる文字列
- N は 1 以上 100 以下の整数
- A_i は 1 以上 10 以下の整数
- S_{i,j} は長さ 1 以上 10 以下の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}
出力
答えを整数として出力せよ。
入力例 1
abcde 3 3 ab abc abcd 4 f c cd bcde 2 e de
出力例 1
2
例えば、以下のようにすると 2 円で最終的な S と T を一致させることができ、これが必要な金額の最低値であることが示せます。
- i=1 について、袋 1 から
abcを選択し S の末尾に連結する。 S=abcとなる。 - i=2 について、何もしない。
- i=3 について、袋 3 から
deを選択し S の末尾に連結する。 S=abcdeとなる。
入力例 2
abcde 3 2 ab abc 3 f c bcde 1 e
出力例 2
-1
どのようにしても最終的な S と T を一致させることができないので、 -1 と出力してください。
入力例 3
aaabbbbcccc 6 2 aa aaa 2 dd ddd 2 ab aabb 4 bbaa bbbc bbb bbcc 2 cc bcc 3 ccc cccc ccccc
出力例 3
4
Score: 425 points
Problem Statement
You initially have an empty string S.
Additionally, there are bags 1, 2, \dots, N, each containing some strings.
Bag i contains A_i strings S_{i,1}, S_{i,2}, \dots, S_{i,A_i}.
You will repeat the following steps for i = 1, 2, \dots, N:
- Choose and perform one of the following two actions:
- Pay 1 yen, select exactly one string from bag i, and concatenate it to the end of S.
- Do nothing.
Given a string T, find the minimum amount of money required to make the final S equal T.
If there is no way to make the final S equal T, print -1.
Constraints
- T is a string consisting of lowercase English letters with length between 1 and 100, inclusive.
- N is an integer between 1 and 100, inclusive.
- A_i is an integer between 1 and 10, inclusive.
- S_{i,j} is a string consisting of lowercase English letters with length between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}
Output
Print the answer as an integer.
Sample Input 1
abcde 3 3 ab abc abcd 4 f c cd bcde 2 e de
Sample Output 1
2
For example, doing the following makes the final S equal T with two yen, which can be shown to be the minimum amount required.
- For i=1, select
abcfrom bag 1 and concatenate it to the end of S, making S=abc. - For i=2, do nothing.
- For i=3, select
defrom bag 3 and concatenate it to the end of S, making S=abcde.
Sample Input 2
abcde 3 2 ab abc 3 f c bcde 1 e
Sample Output 2
-1
There is no way to make the final S equal T, so print -1.
Sample Input 3
aaabbbbcccc 6 2 aa aaa 2 dd ddd 2 ab aabb 4 bbaa bbbc bbb bbcc 2 cc bcc 3 ccc cccc ccccc
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点は 1, \dots, N と番号付けられ、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i, V_i を結んでいます。
それぞれの頂点を赤または青で塗る方法は全部で 2^N 通りあります。これらのうち、以下の条件を全て満たすものの総数を 998244353 で割った余りを求めてください。
- 赤く塗られた頂点がちょうど K 個ある
- 異なる色で塗られた頂点を結ぶ辺の本数は偶数である
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K U_1 V_1 \vdots U_M V_M
出力
答えを出力せよ。
入力例 1
4 4 2 1 2 1 3 2 3 3 4
出力例 1
2
以下の 2 通りが条件を満たします。
- 頂点 1, 2 を赤く、頂点 3, 4 を青く塗る。
- 頂点 3, 4 を赤く、頂点 1, 2 を青く塗る。
上記の塗り方について、異なる色で塗られた頂点を結ぶ辺は 2 番目の辺と 3 番目の辺です。
入力例 2
10 10 3 1 2 2 4 1 5 3 6 3 9 4 10 7 8 9 10 5 9 3 4
出力例 2
64
Score : 500 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertices U_i and V_i.
There are 2^N ways to paint each vertex red or blue. Find the number, modulo 998244353, of such ways that satisfy all of the following conditions:
- There are exactly K vertices painted red.
- There is an even number of edges connecting vertices painted in different colors.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K U_1 V_1 \vdots U_M V_M
Output
Print the answer.
Sample Input 1
4 4 2 1 2 1 3 2 3 3 4
Sample Output 1
2
The following two ways satisfy the conditions.
- Paint Vertices 1 and 2 red and Vertices 3 and 4 blue.
- Paint Vertices 3 and 4 red and Vertices 1 and 2 blue.
In either of the ways above, the 2-nd and 3-rd edges connect vertices painted in different colors.
Sample Input 2
10 10 3 1 2 2 4 1 5 3 6 3 9 4 10 7 8 9 10 5 9 3 4
Sample Output 2
64
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点からなる木が与えられます。 頂点は 1 , 2 , \ldots , N と番号付けられており、 1\leq i\leq N-1 について、i 本目の辺は頂点 U_i と頂点 V_i を結んでいます。 木の直径を D とするとき、木の頂点のうち 2 点以上を選んで赤く塗る方法であって、 赤く塗られたどの頂点の間の距離も D であるようなものの数を 998244353 で割った余りを求めてください。
ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq U_i,V_i \leq N
- U_i \neq V_i
- 入力は全て整数である。
- 与えられるグラフは木である。
入力
入力は以下の形式で標準入力から与えられる。
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
出力
答えを出力せよ。
入力例 1
5 1 2 1 3 1 4 4 5
出力例 1
2
与えられた木は 5 頂点からなり、直径は 3 です。
2 頂点の組であって、その間の距離が 3 であるようなものは (2,5) , (3,5) しか存在しないため、
条件をみたす塗り方は \lbrace 2,5\rbrace と \lbrace 3,5\rbrace の 2 通りとなります。
\lbrace 2,3,5\rbrace は頂点 2 と頂点 3 の間の距離が 2 であるため条件をみたさないことに注意してください。
入力例 2
4 1 2 1 3 1 4
出力例 2
4
直径は 2 であり、条件をみたす塗り方は \lbrace 2,3\rbrace , \lbrace 2,4\rbrace , \lbrace 3,4\rbrace , \lbrace 2,3,4\rbrace の 4 通りとなります。
Score : 500 points
Problem Statement
Given is a tree with N vertices. The vertices are numbered 1, 2, \ldots, N, and for each 1\leq i\leq N-1, the i-th edge connects Vertex U_i and Vertex V_i. Let D be the diameter of the tree. Find the number, modulo 998244353, of ways to choose two or more of the vertices and paint them red so that all distances between two red vertices are D.
Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of the tree is the maximum distance between two vertices.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq U_i,V_i \leq N
- U_i \neq V_i
- All values in input are integers.
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Output
Print the answer.
Sample Input 1
5 1 2 1 3 1 4 4 5
Sample Output 1
2
The given tree has five vertices and a diameter of 3.
There are just two pairs of vertices whose distance is 3: (2,5) and (3,5), so there are two ways to paint the tree to satisfy the condition: \lbrace 2,5\rbrace and \lbrace 3,5\rbrace .
Note that painting 2,3,5 does not satisfy the condition since the distance between Vertex 2 and Vertex 3 is 2.
Sample Input 2
4 1 2 1 3 1 4
Sample Output 2
4
The diameter is 2, and the four ways to paint the tree to satisfy the condition are: \lbrace 2,3\rbrace , \lbrace 2,4\rbrace , \lbrace 3,4\rbrace , \lbrace 2,3,4\rbrace .