Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ 3 の英大文字からなる文字列 S が与えられます。
S の各文字を並び替えることで S を文字列 ABC と一致させることができるか判定してください。
制約
- S は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の各文字を並び替えることで文字列 ABC と一致させることができるなら Yes を、そうでないなら No を出力せよ。
入力例 1
BAC
出力例 1
Yes
S の 1 文字目と S の 2 文字目を入れ替えることで ABC と一致させることができます。
入力例 2
AAC
出力例 2
No
どのように並び替えても S を ABC と一致させることはできません。
入力例 3
ABC
出力例 3
Yes
入力例 4
ARC
出力例 4
No
Score : 100 points
Problem Statement
You are given a string S of length 3 consisting of uppercase English letters.
Determine whether it is possible to rearrange the characters in S to make it match the string ABC.
Constraints
- S is a string of length 3 consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if it is possible to rearrange the characters in S to make it match the string ABC, and No otherwise.
Sample Input 1
BAC
Sample Output 1
Yes
You can make S match ABC by swapping the first and second characters of S.
Sample Input 2
AAC
Sample Output 2
No
You cannot make S match ABC no matter how you rearrange the characters.
Sample Input 3
ABC
Sample Output 3
Yes
Sample Input 4
ARC
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の文字列 S が与えられます。
S のうち先頭に連続する o をすべて取り除いた文字列を出力してください。
なお、 S 中のすべての文字が o である場合は空文字列を出力してください。
制約
- N は 1 \le N \le 50 を満たす整数
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
7 ooparts
出力例 1
parts
ooparts のうち先頭に連続する o をすべて取り除くと parts となります。
入力例 2
6 abcooo
出力例 2
abcooo
先頭の文字が o でない場合もあります。
入力例 3
5 ooooo
出力例 3
すべての文字が o である場合もあります。
Score : 100 points
Problem Statement
You are given a string S of length N.
Output the string obtained by removing all leading consecutive os from S.
If all characters in S are o, output an empty string.
Constraints
- N is an integer satisfying 1 \le N \le 50.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
Output the answer.
Sample Input 1
7 ooparts
Sample Output 1
parts
Removing all leading consecutive os from ooparts gives parts.
Sample Input 2
6 abcooo
Sample Output 2
abcooo
The first character may not be o.
Sample Input 3
5 ooooo
Sample Output 3
All characters may be o.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。
- X の 10^i の位以下を四捨五入する。
- 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
- 具体例を挙げる。
- 273 の 10^1 の位以下を四捨五入すれば 300 となる。
- 999 の 10^2 の位以下を四捨五入すれば 1000 となる。
- 100 の 10^9 の位以下を四捨五入すれば 0 となる。
- 1015 の 10^0 の位以下を四捨五入すれば 1020 となる。
制約
- X,K は整数
- 0 \le X < 10^{15}
- 1 \le K \le 15
入力
入力は以下の形式で標準入力から与えられる。
X K
出力
答えを整数として出力せよ。
入力例 1
2048 2
出力例 1
2100
操作の過程で、 X は 2048 \rightarrow 2050 \rightarrow 2100 と変化します。
入力例 2
1 15
出力例 2
0
入力例 3
999 3
出力例 3
1000
入力例 4
314159265358979 12
出力例 4
314000000000000
X は 32bit 整数型に収まらない可能性があります。
Score : 200 points
Problem Statement
Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.
- Round X off to the nearest 10^i.
- Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
- Here are some examples:
- Rounding 273 off to the nearest 10^2 yields 300.
- Rounding 999 off to the nearest 10^3 yields 1000.
- Rounding 100 off to the nearest 10^{10} yields 0.
- Rounding 1015 off to the nearest 10^1 yields 1020.
Constraints
- X and K are integers.
- 0 \le X < 10^{15}
- 1 \le K \le 15
Input
The input is given from Standard Input in the following format:
X K
Output
Print the answer as an integer.
Sample Input 1
2048 2
Sample Output 1
2100
X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.
Sample Input 2
1 15
Sample Output 2
0
Sample Input 3
999 3
Sample Output 3
1000
Sample Input 4
314159265358979 12
Sample Output 4
314000000000000
X may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字のみからなる N 個の文字列 S_1,S_2,\ldots,S_N が与えられます。
N 個の数字 C_1,C_2,\ldots,C_N を以下のように定義します。
- S_i の先頭の文字が
a,b,cのいずれかならば C_i=2 - S_i の先頭の文字が
d,e,fのいずれかならば C_i=3 - S_i の先頭の文字が
g,h,iのいずれかならば C_i=4 - S_i の先頭の文字が
j,k,lのいずれかならば C_i=5 - S_i の先頭の文字が
m,n,oのいずれかならば C_i=6 - S_i の先頭の文字が
p,q,r,sのいずれかならば C_i=7 - S_i の先頭の文字が
t,u,vのいずれかならば C_i=8 - S_i の先頭の文字が
w,x,y,zのいずれかならば C_i=9
C_1,C_2,\ldots,C_N をこの順に連結した文字列を出力してください。
制約
- 1\leq N\leq 10
- N は整数
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \ldots S_N
出力
C_1,C_2,\ldots,C_N をこの順に連結した文字列を出力せよ。
入力例 1
2 algorithm heuristic
出力例 1
24
- S_1=
algorithmの先頭の文字はaであるため、C_1=2となります。 - S_2=
heuristicの先頭の文字はhであるため、C_2=4となります。
よって、これらを連結した 24 を出力します。
入力例 2
3 i love you
出力例 2
459
Score : 200 points
Problem Statement
You are given N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters.
Define N digits C_1, C_2, \ldots, C_N as follows:
- If the first character of S_i is one of
a,b,c, then C_i=2 - If the first character of S_i is one of
d,e,f, then C_i=3 - If the first character of S_i is one of
g,h,i, then C_i=4 - If the first character of S_i is one of
j,k,l, then C_i=5 - If the first character of S_i is one of
m,n,o, then C_i=6 - If the first character of S_i is one of
p,q,r,s, then C_i=7 - If the first character of S_i is one of
t,u,v, then C_i=8 - If the first character of S_i is one of
w,x,y,z, then C_i=9
Output the string obtained by concatenating C_1, C_2, \ldots, C_N in this order.
Constraints
- 1 \leq N \leq 10
- N is an integer.
- S_i is a string of length between 1 and 10, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \ldots S_N
Output
Output the string obtained by concatenating C_1, C_2, \ldots, C_N in this order.
Sample Input 1
2 algorithm heuristic
Sample Output 1
24
- The first character of S_1=
algorithmisa, so C_1=2. - The first character of S_2=
heuristicish, so C_2=4.
Thus, output 24, which is their concatenation.
Sample Input 2
3 i love you
Sample Output 2
459
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字からなる文字列 S が与えられます。
S の先頭に a をいくつか( 0 個でも良い)つけ加えて回文にすることができるか判定してください。
ただし、長さ N の文字列 A=A_1A_2\ldots A_N が回文であるとは、すべての 1\leq i\leq N について A_i=A_{N+1-i} が成り立っていることをいいます。
制約
- 1 \leq \lvert S \rvert \leq 10^6
- S は英小文字のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の先頭に a をいくつかつけ加えて回文にすることができるならば Yes を、そうでないならば No を出力せよ。
入力例 1
kasaka
出力例 1
Yes
kasaka の先頭に a を 1 つ付け加えることによって、akasaka となり回文となるため Yes を出力します。
入力例 2
atcoder
出力例 2
No
atcoder の先頭に a をいくつ付け加えても回文となる事はありません。
入力例 3
php
出力例 3
Yes
php はそれ自体回文です。S の先頭に付け加える a は 0 個でも許されるため、Yes を出力します。
Score : 300 points
Problem Statement
Given is a string S consisting of lowercase English letters.
Determine whether adding some number of a's (possibly zero) at the beginning of S can make it a palindrome.
Here, a string of length N, A=A_1A_2\ldots A_N, is said to be a palindrome when A_i=A_{N+1-i} for every 1\leq i\leq N.
Constraints
- 1 \leq \lvert S \rvert \leq 10^6
- S consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
If adding some number of a's (possibly zero) at the beginning of S can make it a palindrome, print Yes; otherwise, print No.
Sample Input 1
kasaka
Sample Output 1
Yes
By adding one a at the beginning of kasaka, we have akasaka, which is a palindrome, so Yes should be printed.
Sample Input 2
atcoder
Sample Output 2
No
Adding any number of a's at the beginning of atcoder does not make it a palindrome.
Sample Input 3
php
Sample Output 3
Yes
php itself is a palindrome. Adding zero a's at the beginning of S is allowed, so Yes should be printed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 H マス, 横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と呼びます。
はじめ、グリッド上には、ある 縦横 2 マス以上 の部分長方形の内部にあるマスにクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていません。
形式的に説明すると、以下の条件を全て満たす 4 つの整数の組 (a,b,c,d) がただ 1 つ存在します。
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- グリッド上のマスのうち、a \leq i \leq b, c \leq j \leq d を満たす全てのマス (i, j) にはクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていない。
ところが、すぬけ君がグリッド上のクッキーのどれか 1 枚を取って食べてしまいました。
すぬけ君がクッキーを取ったマスは、クッキーが置かれていない状態に変わります。
すぬけ君がクッキーを食べた後のグリッドの状態が入力として与えられます。
マス (i, j) の状態は文字 S_{i,j} として与えられて、# はクッキーが置かれているマスを, . はクッキーが置かれていないマスを意味します。
すぬけ君が食べたクッキーが元々置かれていたマスを答えてください。(答えは一意に定まります。)
制約
- 2 \leq H, W \leq 500
- S_{i,j} は
#または.
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
出力
すぬけ君が食べたクッキーが元々置かれていたマスを (i, j) とする。i, j をこの順に空白区切りで出力せよ。
入力例 1
5 6 ...... ..#.#. ..###. ..###. ......
出力例 1
2 4
はじめ、クッキーは (2, 3) を左上、(4, 5) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (2, 4) にあるクッキーを食べたことがわかります。よって (2, 4) を出力します。
入力例 2
3 2 #. ## ##
出力例 2
1 2
はじめ、クッキーは (1, 1) を左上、(3, 2) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (1, 2) にあるクッキーを食べたことがわかります。
入力例 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
出力例 3
2 5
Score : 300 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.
Initially, there was one cookie on each square inside a rectangle whose height and width were at least 2 squares long, and no cookie on the other squares.
Formally, there was exactly one quadruple of integers (a,b,c,d) that satisfied all of the following conditions.
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- There was one cookie on each square (i, j) such that a \leq i \leq b, c \leq j \leq d, and no cookie on the other squares.
However, Snuke took and ate one of the cookies on the grid.
The square that contained that cookie is now empty.
As the input, you are given the state of the grid after Snuke ate the cookie.
The state of the square (i, j) is given as the character S_{i,j}, where # means a square with a cookie, and . means a square without one.
Find the square that contained the cookie eaten by Snuke. (The answer is uniquely determined.)
Constraints
- 2 \leq H, W \leq 500
- S_{i,j} is
#or..
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
Output
Let (i, j) the square contained the cookie eaten by Snuke. Print i and j in this order, separated by a space.
Sample Input 1
5 6 ...... ..#.#. ..###. ..###. ......
Sample Output 1
2 4
Initially, cookies were on the squares inside the rectangle with (2, 3) as the top-left corner and (4, 5) as the bottom-right corner, and Snuke ate the cookie on (2, 4). Thus, you should print (2, 4).
Sample Input 2
3 2 #. ## ##
Sample Output 2
1 2
Initially, cookies were placed on the squares inside the rectangle with (1, 1) as the top-left corner and (3, 2) as the bottom-right corner, and Snuke ate the cookie at (1, 2).
Sample Input 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
Sample Output 3
2 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
AtCoder 社の壁紙の模様を xy 平面上に表現すると、以下のようになります。
-
以下の 3 種類の直線で領域が分割されている。
- x = n (n は整数)
- y = n (n は偶数)
- x + y = n (n は偶数)
- 各領域は白もしくは黒で塗られている。いずれかの直線で隣接する 2 領域は異なる色で塗られている。
- (0.5, 0.5) を含む領域は黒で塗られている。
下の図は、模様の一部を表したものです。

整数 A, B, C, D が与えられます。各辺が x, y 軸に平行で、左下の頂点が (A, B) にあり右上の頂点が (C, D) にあるような長方形を考えます。この長方形の内側に存在する黒で塗られた領域の面積を求め、それを 2 倍したものを出力してください。
出力する値は整数になることが証明できます。
制約
- -10^9 \leq A, B, C, D \leq 10^9
- A < C かつ B < D
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
答えを一行に出力せよ。
入力例 1
0 0 3 3
出力例 1
10
求めるのは、以下の正方形で囲われた領域内の黒く塗られた領域の面積です。

これは 5 なので、 2 倍した 10 を出力します。
入力例 2
-1 -2 1 3
出力例 2
11
面積は 5.5 と小数になりますが、出力するべき値は整数になります。
入力例 3
-1000000000 -1000000000 1000000000 1000000000
出力例 3
4000000000000000000
これは長方形が最大のケースですが、出力は 64bit 符号付き整数の範囲に収まります。
Score : 450 points
Problem Statement
The pattern of AtCoder's wallpaper can be represented on the xy-plane as follows:
-
The plane is divided by the following three types of lines:
- x = n (where n is an integer)
- y = n (where n is an even number)
- x + y = n (where n is an even number)
- Each region is painted black or white. Any two regions adjacent along one of these lines are painted in different colors.
- The region containing (0.5, 0.5) is painted black.
The following figure shows a part of the pattern.

You are given integers A, B, C, D. Consider a rectangle whose sides are parallel to the x- and y-axes, with its bottom-left vertex at (A, B) and its top-right vertex at (C, D). Calculate the area of the regions painted black inside this rectangle, and print twice that area.
It can be proved that the output value will be an integer.
Constraints
- -10^9 \leq A, B, C, D \leq 10^9
- A < C and B < D.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B C D
Output
Print the answer on a single line.
Sample Input 1
0 0 3 3
Sample Output 1
10
We are to find the area of the black-painted region inside the following square:

The area is 5, so print twice that value: 10.
Sample Input 2
-1 -2 1 3
Sample Output 2
11
The area is 5.5, which is not an integer, but the output value is an integer.
Sample Input 3
-1000000000 -1000000000 1000000000 1000000000
Sample Output 3
4000000000000000000
This is the case with the largest rectangle, where the output still fits into a 64-bit signed integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
2 行 L 列のマス目があります。 上から i 行目 (i\in\lbrace1,2\rbrace)、左から j 列目 (1\leq j\leq L)のマス目を (i,j) で表します。 (i,j) には整数 x _ {i,j} が書かれています。
x _ {1,j}=x _ {2,j} であるような整数 j の個数を求めてください。
ただし、x _ {i,j} の情報は (x _ {1,1},x _ {1,2},\ldots,x _ {1,L}) と (x _ {2,1},x _ {2,2},\ldots,x _ {2,L}) をそれぞれ連長圧縮した、長さ N _ 1 の列 ((v _ {1,1},l _ {1,1}),\ldots,(v _ {1,N _ 1},l _ {1,N _ 1})) と長さ N _ 2 の列 ((v _ {2,1},l _ {2,1}),\ldots,(v _ {2,N _ 2},l _ {2,N _ 2})) として与えられます。
ここで、列 A の連長圧縮とは、A の要素 v _ i と正整数 l _ i の組 (v _ i,l _ i) の列であって、次の操作で得られるものです。
- A を異なる要素が隣り合っている部分で分割する。
- 分割した各列 B _ 1,B _ 2,\ldots,B _ k に対して、v _ i を B _ i の要素、l _ i を B _ i の長さとする。
制約
- 1\leq L\leq 10 ^ {12}
- 1\leq N _ 1,N _ 2\leq 10 ^ 5
- 1\leq v _ {i,j}\leq 10 ^ 9\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- 1\leq l _ {i,j}\leq L\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- v _ {i,j}\neq v _ {i,j+1}\ (i\in\lbrace1,2\rbrace,1\leq j\lt N _ i)
- l _ {i,1}+l _ {i,2}+\cdots+l _ {i,N _ i}=L\ (i\in\lbrace1,2\rbrace)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
L N _ 1 N _ 2
v _ {1,1} l _ {1,1}
v _ {1,2} l _ {1,2}
\vdots
v _ {1,N _ 1} l _ {1,N _ 1}
v _ {2,1} l _ {2,1}
v _ {2,2} l _ {2,2}
\vdots
v _ {2,N _ 2} l _ {2,N _ 2}
出力
答えを 1 行で出力せよ。
入力例 1
8 4 3 1 2 3 2 2 3 3 1 1 4 2 1 3 3
出力例 1
4
マス目は以下の図のようになっています。

x _ {1,j}=x _ {2,j} となるような整数 j は、j=1,2,5,8 の 4 つなので、出力すべき値は 4 です。
入力例 2
10000000000 1 1 1 10000000000 1 10000000000
出力例 2
10000000000
答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。
入力例 3
1000 4 7 19 79 33 463 19 178 33 280 19 255 33 92 34 25 19 96 12 11 19 490 33 31
出力例 3
380
Score : 500 points
Problem Statement
We have a grid with 2 rows and L columns. Let (i,j) denote the square at the i-th row from the top (i\in\lbrace1,2\rbrace) and j-th column from the left (1\leq j\leq L). (i,j) has an integer x _ {i,j} written on it.
Find the number of integers j such that x _ {1,j}=x _ {2,j}.
Here, the description of x _ {i,j} is given to you as the run-length compressions of (x _ {1,1},x _ {1,2},\ldots,x _ {1,L}) and (x _ {2,1},x _ {2,2},\ldots,x _ {2,L}) into sequences of lengths N _ 1 and N _ 2, respectively: ((v _ {1,1},l _ {1,1}),\ldots,(v _ {1,N _ 1},l _ {1,N _ 1})) and ((v _ {2,1},l _ {2,1}),\ldots,(v _ {2,N _ 2},l _ {2,N _ 2})).
Here, the run-length compression of a sequence A is a sequence of pairs (v _ i,l _ i) of an element v _ i of A and a positive integer l _ i obtained as follows.
- Split A between each pair of different adjacent elements.
- For each sequence B _ 1,B _ 2,\ldots,B _ k after the split, let v _ i be the element of B _ i and l _ i be the length of B _ i.
Constraints
- 1\leq L\leq 10 ^ {12}
- 1\leq N _ 1,N _ 2\leq 10 ^ 5
- 1\leq v _ {i,j}\leq 10 ^ 9\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- 1\leq l _ {i,j}\leq L\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)
- v _ {i,j}\neq v _ {i,j+1}\ (i\in\lbrace1,2\rbrace,1\leq j\lt N _ i)
- l _ {i,1}+l _ {i,2}+\cdots+l _ {i,N _ i}=L\ (i\in\lbrace1,2\rbrace)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
L N _ 1 N _ 2
v _ {1,1} l _ {1,1}
v _ {1,2} l _ {1,2}
\vdots
v _ {1,N _ 1} l _ {1,N _ 1}
v _ {2,1} l _ {2,1}
v _ {2,2} l _ {2,2}
\vdots
v _ {2,N _ 2} l _ {2,N _ 2}
Output
Print a single line containing the answer.
Sample Input 1
8 4 3 1 2 3 2 2 3 3 1 1 4 2 1 3 3
Sample Output 1
4
The grid is shown below.

We have four integers j such that x _ {1,j}=x _ {2,j}: j=1,2,5,8. Thus, you should print 4.
Sample Input 2
10000000000 1 1 1 10000000000 1 10000000000
Sample Output 2
10000000000
Note that the answer may not fit into a 32-bit integer.
Sample Input 3
1000 4 7 19 79 33 463 19 178 33 280 19 255 33 92 34 25 19 96 12 11 19 490 33 31
Sample Output 3
380
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の無向木 T が与えられます。頂点には 1, 2, \ldots, N の番号が付いており、i 番目の辺は頂点 A_i と頂点 B_i を結ぶ無向辺です。
グラフがアルカンであるとは以下の条件をともに満たしていることであると定義します。
- グラフは無向木である
- すべての頂点の次数が 1 または 4 であり、次数 4 の頂点が 1 つ以上存在する
T の部分グラフであってアルカンであるものが存在するか判定し、存在する場合はそのようなものの頂点数の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- 与えられるグラフは無向木
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
出力
T の部分グラフであってアルカンであるものが存在する場合はそのようなものの頂点数の最大値を出力せよ。そうでない場合は -1 と出力せよ。
入力例 1
9 1 2 2 3 3 4 4 5 2 6 2 7 3 8 3 9
出力例 1
8
頂点 u と頂点 v を結ぶ無向辺を辺 (u, v) と表記します。
頂点 1,2,3,4,6,7,8,9、辺 (1,2),(2,3),(3,4),(2,6),(2,7),(3,8),(3,9) からなる部分グラフはアルカンです。
入力例 2
7 1 2 1 3 2 4 2 5 3 6 3 7
出力例 2
-1
入力例 3
15 8 5 2 9 1 12 6 11 9 3 15 1 7 12 7 13 10 5 6 9 5 1 1 9 4 5 6 14
出力例 3
11
Score : 500 points
Problem Statement
You are given an undirected tree T with N vertices, numbered 1, 2, \ldots, N. The i-th edge is an undirected edge connecting vertices A_i and B_i.
A graph is defined to be an alkane if and only if it satisfies the following conditions:
- The graph is an undirected tree.
- Every vertex has degree 1 or 4, and there is at least one vertex of degree 4.
Determine whether there exists a subgraph of T that is an alkane, and if so, find the maximum number of vertices in such a subgraph.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- The given graph is an undirected tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
Output
If there exists a subgraph of T that is an alkane, print the maximum number of vertices in such a subgraph. Otherwise, print -1.
Sample Input 1
9 1 2 2 3 3 4 4 5 2 6 2 7 3 8 3 9
Sample Output 1
8
Let (u, v) denote an undirected edge between vertices u and v.
A subgraph consisting of vertices 1,2,3,4,6,7,8,9 and edges (1,2),(2,3),(3,4),(2,6),(2,7),(3,8),(3,9) is an alkane.
Sample Input 2
7 1 2 1 3 2 4 2 5 3 6 3 7
Sample Output 2
-1
Sample Input 3
15 8 5 2 9 1 12 6 11 9 3 15 1 7 12 7 13 10 5 6 9 5 1 1 9 4 5 6 14
Sample Output 3
11