Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
1 以上 9 以下の整数 N が入力として与えられます。
数字 N を N 個繋げて得られる文字列を出力してください。
制約
- N は 1 以上 9 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
3
出力例 1
333
3 を 3 個繋げて得られる文字列は 333 です。
入力例 2
9
出力例 2
999999999
Score : 100 points
Problem Statement
You are given an integer N between 1 and 9, inclusive, as input.
Concatenate N copies of the digit N and print the resulting string.
Constraints
- N is an integer between 1 and 9, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
3
Sample Output 1
333
Concatenate three copies of the digit 3 to yield the string 333.
Sample Input 2
9
Sample Output 2
999999999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君は、レストランで「AtCoder ドリンク」というドリンクを飲もうとしています。 AtCoder ドリンクは定価である P 円を払えば飲むことができます。
また、高橋君は割引券を持っており、それを使うと AtCoder ドリンクを定価より安い価格である Q 円で飲むことができますが、 その場合には AtCoder ドリンクの他に、N 品ある料理の中から 1 つを追加で注文しなければなりません。 i = 1, 2, \ldots, N について、i 番目の料理の価格は D_i 円です。
高橋君がドリンクを飲むため支払う合計金額の最小値を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq Q \lt P \leq 10^5
- 1 \leq D_i \leq 10^5
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P Q D_1 D_2 \ldots D_N
出力
答えを出力せよ。
入力例 1
3 100 50 60 20 40
出力例 1
70
割引券を使用して 2 番目の料理を注文することで、ドリンク代 50 円と料理代 20 円の合計 70 円の支払いで AtCoder ドリンクを飲むことができ、支払う合計金額が最小となります。
入力例 2
3 100 50 60000 20000 40000
出力例 2
100
割引券を使用せず定価の 100 円で AtCoder ドリンクを飲むことで、支払う合計金額が最小となります。
Score : 100 points
Problem Statement
Takahashi wants a beverage called AtCoder Drink in a restaurant. It can be ordered at a regular price of P yen.
He also has a discount coupon that allows him to order it at a lower price of Q yen. However, he must additionally order one of the restaurant's N dishes to use that coupon. For each i = 1, 2, \ldots, N, the price of the i-th dish is D_i yen.
Print the minimum total amount of money that he must pay to get the drink.
Constraints
- 1 \leq N \leq 100
- 1 \leq Q \lt P \leq 10^5
- 1 \leq D_i \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P Q D_1 D_2 \ldots D_N
Output
Print the answer.
Sample Input 1
3 100 50 60 20 40
Sample Output 1
70
If he uses the coupon and orders the second dish, he can get the drink by paying 50 yen for it and 20 yen for the dish, for a total of 70 yen, which is the minimum total payment needed.
Sample Input 2
3 100 50 60000 20000 40000
Sample Output 2
100
The total payment will be minimized by not using the coupon and paying the regular price of 100 yen.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 個のボールがあります。
i 番目のボールの色は C_i、大きさは S_i です。ここで、色は 1, 2, \ldots, M の整数で表されます。
k = 1, 2, \ldots, M について、色 k のボールの大きさの最大値を出力してください。ただし、色 k のボールが存在しない場合は -1 と出力してください。
制約
- 1 \leq N, M \leq 100
- 1 \leq C_i \leq M
- 1 \leq S_i \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M C_1 S_1 C_2 S_2 \vdots C_N S_N
出力
k = 1, 2, \ldots, M の順に、色 k のボールが存在するならば色 k のボールの大きさの最大値を、存在しないならば -1 を空白区切りで出力せよ。
入力例 1
4 5 1 3 2 10 1 7 4 9
出力例 1
7 10 -1 9 -1
色 1 のボールの大きさの最大値は 7、色 2 のボールの大きさの最大値は 10、色 4 のボールの大きさの最大値は 9 です。
色 3 のボール、色 5 のボールは存在しません。
入力例 2
5 5 2 6 5 12 5 2 5 9 2 7
出力例 2
-1 7 -1 -1 12
Score : 200 points
Problem Statement
There are N balls.
The color of the i-th ball is C_i, and its size is S_i. Here, the colors are represented by integers 1, 2, \ldots, M.
For k = 1, 2, \ldots, M, output the maximum size of a ball of color k, or -1 if there is no ball of color k.
Constraints
- 1 \leq N, M \leq 100
- 1 \leq C_i \leq M
- 1 \leq S_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M C_1 S_1 C_2 S_2 \vdots C_N S_N
Output
In the order k = 1, 2, \ldots, M, output the maximum size of a ball of color k if such a ball exists, or -1 otherwise, separated by spaces.
Sample Input 1
4 5 1 3 2 10 1 7 4 9
Sample Output 1
7 10 -1 9 -1
The maximum size of a ball of color 1 is 7, that of a ball of color 2 is 10, and that of a ball of color 4 is 9.
There is no ball of color 3 or color 5.
Sample Input 2
5 5 2 6 5 12 5 2 5 9 2 7
Sample Output 2
-1 7 -1 -1 12
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
ここに円形のピザが 1 枚あります。
高橋くんは長さ N の数列 A を使ってこのピザを以下の手順で切り分けます。
- 最初に、円の中心から 12 時の方向に切れ込みをひとつ入れます。
- 次に、以下の操作を N 回繰り返します。 i 回目の操作では以下を行います。
- まず、ピザを時計回りに A_i 度回転させる。
- 次に、円の中心から 12 時の方向に切れ込みをひとつ入れる。
例えば、A=(90,180,45,195) として手順を行うと、下図のようになります。

このとき、最も大きなピザの中心角が何度であるか求めてください。
制約
- 入力は全て整数
- 1 \le N \le 359
- 1 \le A_i \le 359
- 同じ場所に複数回切れ込みが入ることはない。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 90 180 45 195
出力例 1
120
この入力は問題文中の例と一致します。
最も大きなピザの中心角は 120 度です。
入力例 2
1 1
出力例 2
359
入力例 3
10 215 137 320 339 341 41 44 18 241 149
出力例 3
170
Score : 200 points
Problem Statement
We have a circular pizza.
Takahashi will cut this pizza using a sequence A of length N, according to the following procedure.
- First, make a cut from the center in the 12 o'clock direction.
- Next, do N operations. The i-th operation is as follows.
- Rotate the pizza A_i degrees clockwise.
- Then, make a cut from the center in the 12 o'clock direction.
For example, if A=(90,180,45,195), the procedure cuts the pizza as follows.

Find the center angle of the largest pizza after the procedure.
Constraints
- All values in input are integers.
- 1 \le N \le 359
- 1 \le A_i \le 359
- There will be no multiple cuts at the same position.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
4 90 180 45 195
Sample Output 1
120
This input coincides with the example in the Problem Statement.
The center angle of the largest pizza is 120 degrees.
Sample Input 2
1 1
Sample Output 2
359
Sample Input 3
10 215 137 320 339 341 41 44 18 241 149
Sample Output 3
170
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
以下の条件を満たす正整数 x を 321-like Number と呼びます。 この定義は A 問題と同様です。
- x の各桁を上から見ると狭義単調減少になっている。
- すなわち、x が d 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
- ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )
なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。
例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。
K 番目に小さい 321-like Number を求めてください。
制約
- 入力は全て整数
- 1 \le K
- 321-like Number は K 個以上存在する
入力
入力は以下の形式で標準入力から与えられる。
K
出力
K 番目に小さい 321-like Number を整数として出力せよ。
入力例 1
15
出力例 1
32
321-like Number は小さいものから順に (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) です。
このうち 15 番目に小さいものは 32 です。
入力例 2
321
出力例 2
9610
入力例 3
777
出力例 3
983210
Score : 300 points
Problem Statement
A positive integer x is called a 321-like Number when it satisfies the following condition. This definition is the same as the one in Problem A.
- The digits of x are strictly decreasing from top to bottom.
- In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
- (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).
Note that all one-digit positive integers are 321-like Numbers.
For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.
Find the K-th smallest 321-like Number.
Constraints
- All input values are integers.
- 1 \le K
- At least K 321-like Numbers exist.
Input
The input is given from Standard Input in the following format:
K
Output
Print the K-th smallest 321-like Number as an integer.
Sample Input 1
15
Sample Output 1
32
The 321-like Numbers are (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) from smallest to largest.
The 15-th smallest of them is 32.
Sample Input 2
321
Sample Output 2
9610
Sample Input 3
777
Sample Output 3
983210
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる長さ N の文字列 S = S_1S_2\ldots S_N が与えられます。
また、S に関する Q 個の質問が与えられます。 i = 1, 2, \ldots, Q について、i 番目の質問は 2 つの整数 l_i, r_i で表される下記の質問です。
S の l_i 文字目から r_i 文字目までからなる部分文字列 S_{l_i}S_{l_i+1}\ldots S_{r_i} において、 同じ英小文字が 2 つ隣りあう箇所は何個ありますか? すなわち、l_i \leq p \leq r_i-1 かつ S_p = S_{p+1}を満たす整数 p は何個ありますか?
Q 個の質問それぞれの答えを出力してください。
制約
- N, Q は整数
- 1 \leq N, Q \leq 3 \times 10^5
- S は英小文字のみからなる長さ N の文字列
- l_i, r_i は整数
- 1 \leq l_i \leq r_i \leq N
入力
入力は以下の形式で標準入力から与えられる。
N Q S l_1 r_1 l_2 r_2 \vdots l_Q r_Q
出力
Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には i 番目の質問に対する答えを出力せよ。
入力例 1
11 4 mississippi 3 9 4 10 4 6 7 7
出力例 1
2 2 0 0
4 個の質問それぞれに対する答えは下記の通りです。
- 1 個目の質問に関して、S_3S_4\ldots S_9 =
ssissipで同じ英小文字が隣り合う箇所は、S_3S_4 =ssと S_6S_7 =ssの 2 個です。 - 2 個目の質問に関して、S_4S_5\ldots S_{10} =
sissippで同じ英小文字が隣り合う箇所は、S_6S_7 =ssと S_9S_{10} =ppの 2 個です。 - 3 個目の質問に関して、S_4S_5S_6 =
sisで同じ英小文字が隣り合う箇所は 0 個です。 - 4 個目の質問に関して、S_7 =
sで同じ英小文字が隣り合う箇所は 0 個です。
入力例 2
5 1 aaaaa 1 5
出力例 2
4
S_1S_2\ldots S_5 = aaaaa で同じ英小文字が隣り合う箇所は、
S_1S_2 = aa 、S_2S_3 = aa 、S_3S_4 = aa 、S_4S_5 = aa の 4 個です。
Score : 300 points
Problem Statement
You are given a string S = S_1S_2\ldots S_N of length N consisting of lowercase English letters.
Additionally, you are given Q queries about the string S. For i = 1, 2, \ldots, Q, the i-th query is represented by two integers l_i, r_i and asks the following.
In the substring S_{l_i}S_{l_i+1}\ldots S_{r_i} of S, which ranges from the l_i-th to the r_i-th character, how many places are there where the same lowercase English letter occurs twice in a row? In other words, how many integers p satisfy l_i \leq p \leq r_i-1 and S_p = S_{p+1}?
Print the answer for each of the Q queries.
Constraints
- N and Q are integers.
- 1 \leq N, Q \leq 3 \times 10^5
- S is a string of length N consisting of lowercase English letters.
- l_i and r_i are integers.
- 1 \leq l_i \leq r_i \leq N
Input
The input is given from Standard Input in the following format:
N Q S l_1 r_1 l_2 r_2 \vdots l_Q r_Q
Output
Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th query.
Sample Input 1
11 4 mississippi 3 9 4 10 4 6 7 7
Sample Output 1
2 2 0 0
The answers to the four queries are as follows.
- For the first query, S_3S_4\ldots S_9 =
ssissiphas two places where the same lowercase English letter occurs twice in a row: S_3S_4 =ssand S_6S_7 =ss. - For the second query, S_4S_5\ldots S_{10} =
sissipphas two places where the same lowercase English letter occurs twice in a row: S_6S_7 =ssand S_9S_{10} =pp. - For the third query, S_4S_5S_6 =
sishas zero places where the same lowercase English letter occurs twice in a row. - For the fourth query, S_7 =
shas zero places where the same lowercase English letter occurs twice in a row.
Sample Input 2
5 1 aaaaa 1 5
Sample Output 2
4
S_1S_2\ldots S_5 = aaaaa has four places where the same lowercase English letter occurs twice in a row:
S_1S_2 = aa, S_2S_3 = aa, S_3S_4 = aa, and S_4S_5 = aa.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
これから N 日間の天気が文字列 S として与えられます。
S の i 文字目が S であるとき i 日目の天気は晴れ、 R であるとき i 日目の天気は雨です。
また、現時点では嬉しさは 0 です。
あなたは、以下の操作を 0 回以上何回でも行えます。
- 1 以上 N 以下の整数 i を選ぶ。
- i 日目の天気が晴れであれば雨、雨であれば晴れに変更する。
- ただし、 i 日目の天気を変更した場合、嬉しさが X_i 減少する。
操作を行った後の最終的な天気に対して、以下の条件で嬉しさが増加します。
- 1 \le i \le N-1 を満たす各整数 i について、変更後の i 日目の天気が雨、かつ i+1 日目の天気が晴れであるとき、嬉しさが Y_i 増加する。
操作を行った結果として、達成可能な嬉しさの最大値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \le T \le 10^4
- N は 2 以上 2 \times 10^5 以下の整数
- S は長さ N の
SとRからなる文字列 - X_i は 1 以上 10^9 以下の整数
- Y_i は 1 以上 10^9 以下の整数
- ひとつの入力における N の総和は 2 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 個目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N
S
X_1 X_2 \dots X_N
Y_1 Y_2 \dots Y_{N-1}
出力
T 行出力せよ。i 行目には i 個目のテストケースの答えを出力せよ。
入力例 1
5 6 SRRRSR 3 1 4 1 5 9 2 6 5 3 5 6 RSRSRS 10 10 10 10 10 10 1 1 1 1 1 2 RR 4 3 2 10 RSSRSSRSSR 75 49 79 37 16 9 38 49 69 54 23 100 73 63 66 23 51 65 67 20 SSSRSSSRRRRSSRSSRSSR 343191362 223147518 135066250 426658267 693515093 8023388 383375974 712283203 40447501 19870690 318452142 356265717 283999278 209219229 418603824 39363351 392058270 254796273 110117486 64951139 576697130 385986330 895027325 654885799 784214084 577658764 761714876 583039741 943991250 446493376 701505924 402891440 963636095 919408713 238125227 871191978 843843821 397910552 529447424
出力例 1
5 3 0 165 5201284760
この入力には 5 個のテストケースが含まれています。
1 個目のテストケースについて、例えば以下の通りに操作を行うことで嬉しさを最大とすることができます。
- 3 日目の天気を雨から晴れに変更する。嬉しさが X_3=4 減少し、各日の天気は 晴れ、雨、晴れ、雨、晴れ、雨 となります。
- この操作の結果、 2 日目が雨、 3 日目が晴れであることから嬉しさが Y_2=6 増加し、 4 日目が雨、 5 日目が晴れであることから嬉しさが Y_4=3 増加します。
- 全体の嬉しさは (-4)+6+3 = 5 であり、これが達成可能な最大です。
2,3 個目のテストケースについて、操作を行わないことが最適である場合があります。
5 個目のテストケースについて、答えが 32bit 整数型に収まらない場合があることに注意してください。
Score : 400 points
Problem Statement
The weather for the upcoming N days is given as a string S.
If the i-th character of S is S, then the weather on day i is sunny; if it is R, then the weather on day i is rainy.
Also, your current happiness is 0.
You can perform the following operation any number of times, possibly zero:
- Choose an integer i with 1 \le i \le N.
- If the weather on day i is sunny, change it to rainy; if it is rainy, change it to sunny.
- However, if you change the weather on day i, your happiness decreases by X_i.
After performing the operations, your happiness increases based on the final weather according to the following condition:
- For each integer i with 1 \le i \le N-1, if the weather on day i after modification is rainy and the weather on day i+1 is sunny, your happiness increases by Y_i.
Find the maximum value of happiness achievable by performing operations.
T test cases are given; solve each.
Constraints
- 1 \le T \le 10^4
- N is an integer between 2 and 2 \times 10^5, inclusive.
- S is a string of length N consisting of
SandR. - X_i is an integer between 1 and 10^9, inclusive.
- Y_i is an integer between 1 and 10^9, inclusive.
- The sum of N in a single input is at most 2 \times 10^5.
Input
The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N
S
X_1 X_2 \dots X_N
Y_1 Y_2 \dots Y_{N-1}
Output
Output T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
5 6 SRRRSR 3 1 4 1 5 9 2 6 5 3 5 6 RSRSRS 10 10 10 10 10 10 1 1 1 1 1 2 RR 4 3 2 10 RSSRSSRSSR 75 49 79 37 16 9 38 49 69 54 23 100 73 63 66 23 51 65 67 20 SSSRSSSRRRRSSRSSRSSR 343191362 223147518 135066250 426658267 693515093 8023388 383375974 712283203 40447501 19870690 318452142 356265717 283999278 209219229 418603824 39363351 392058270 254796273 110117486 64951139 576697130 385986330 895027325 654885799 784214084 577658764 761714876 583039741 943991250 446493376 701505924 402891440 963636095 919408713 238125227 871191978 843843821 397910552 529447424
Sample Output 1
5 3 0 165 5201284760
This input contains five test cases.
For the first test case, for example, you can maximize happiness by performing the following operations:
- Change the weather on day 3 from rainy to sunny. Happiness decreases by X_3 = 4, and the weather on each day becomes sunny, rainy, sunny, rainy, sunny, rainy.
- As a result of this operation, since day 2 is rainy and day 3 is sunny, happiness increases by Y_2 = 6, and since day 4 is rainy and day 5 is sunny, happiness increases by Y_4 = 3.
- The total happiness is (-4) + 6 + 3 = 5, which is the achievable maximum.
For the second and third test cases, performing no operations may be optimal.
For the fifth test case, note that the answer may not fit in a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
o と x からなる長さ N の文字列 S が与えられます。
ただし、S には o が K 個以上含まれることが保証されます。
高橋君はあるゲームを N 回行いました。
i 回目のゲームでは、S の i 文字目が o ならば高橋君は勝利し、x ならば高橋君は敗北しました。
高橋君は以下の条件を満たすような 2 整数 l,r を一つ選びます。
- 1 \leq l \leq r \leq N
- l 回目から r 回目までのゲームで K 勝以上している
このとき、l 回目から r 回目までのゲームでの勝率としてあり得る値の最大値を求めてください。
制約
- 1 \leq K \leq N \leq 10^6
- N と K は整数
- S は
oとxからなる長さ N の文字列 - S は
oを K 個以上含む
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを 1 行で出力せよ。 真の答えとの絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。
入力例 1
10 4 oxooxoxxox
出力例 1
0.6666666666
(l,r) として (1,6) を選ぶと勝率は \frac{2}{3} です。
条件を満たす範囲で勝率をこれより大きくすることはできません。
入力例 2
5 1 xxoxx
出力例 2
1
入力例 3
16 10 xxxoxooooxoxoooo
出力例 3
0.769230769230769
Score : 475 points
Problem Statement
You are given a string S of length N consisting of o and x.
It is guaranteed that S contains at least K occurrences of o.
Takahashi played a certain game N times.
In the i-th game, he won if the i-th character of S is o, and lost if it is x.
Takahashi chooses a pair of integers l and r satisfying the following conditions.
- 1 \leq l \leq r \leq N
- He won at least K times in the games from the l-th through the r-th.
Find the maximum possible value of the win rate in the games from the l-th through the r-th.
Constraints
- 1 \leq K \leq N \leq 10^6
- N and K are integers.
- S is a string of length N consisting of
oandx. - S contains at least K occurrences of
o.
Input
The input is given from Standard Input in the following format:
N K S
Output
Output the answer in one line. Answers with an absolute or relative error of at most 10^{-6} from the true answer will be accepted.
Sample Input 1
10 4 oxooxoxxox
Sample Output 1
0.6666666666
Choosing (1,6) as (l,r) gives a win rate of \frac{2}{3}.
It is impossible to make the win rate larger than this while satisfying the conditions.
Sample Input 2
5 1 xxoxx
Sample Output 2
1
Sample Input 3
16 10 xxxoxooooxoxoooo
Sample Output 3
0.769230769230769
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N までの番号が付いた、N 頂点 0 辺の無向グラフ G があります。
Q 個のクエリが与えられます。i 番目のクエリでは、グラフ G に頂点 u_i と頂点 v_i を結ぶ辺を追加します。
それぞれのクエリを処理した直後のグラフ G において、以下の条件を満たすように G の各頂点を白または黒のどちらか一色で塗ることができるか判定し、可能な場合は黒に塗る頂点の個数としてありうる最小値を求めてください。
- どの辺についても、両端の頂点が異なる色で塗られている。
制約
- 2 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- 1 \le u_i \lt v_i \le N
- (u_i, v_i) \ne (u_j, v_j) \ (i \ne j)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q u_1 v_1 u_2 v_2 \vdots u_Q v_Q
出力
Q 行出力せよ。
i 行目には i 番目のクエリを処理した直後のグラフ G について、以下の値を出力せよ。
- 条件を満たす塗り方が可能な場合、黒に塗る頂点の個数としてありうる最小値
- 条件を満たす塗り方が不可能な場合、 -1
入力例 1
4 4 1 2 2 3 3 4 1 3
出力例 1
1 1 2 -1
1, 2, 3 番目のクエリを処理した直後については、条件を満たす塗り方が存在します。
例えば、 3 番目のクエリを処理した直後は、頂点 1, 2, 3, 4 をそれぞれ 白 黒 白 黒 と塗ることができます。これより 黒 に塗る頂点の個数が少なく、条件を満たす塗り方は存在しないので 2 を出力してください。
4 番目のクエリを処理した直後は、条件を満たすように塗ることができません。よって -1 を出力してください。
入力例 2
10 10 1 10 6 7 2 7 4 9 5 9 6 10 7 8 2 5 3 4 8 10
出力例 2
1 2 2 3 3 3 3 4 4 4
Score : 500 points
Problem Statement
There is an undirected graph G with N vertices numbered 1 through N and zero edges.
Q queries are given. In the i-th query, an edge connecting vertices u_i and v_i is added to graph G.
Immediately after processing each query, determine whether it is possible to color each vertex of G white or black so that the following condition is satisfied, and if possible, find the minimum possible number of vertices colored black.
- For every edge, the two endpoints have different colors.
Constraints
- 2 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- 1 \le u_i \lt v_i \le N
- (u_i, v_i) \ne (u_j, v_j) \ (i \ne j)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q u_1 v_1 u_2 v_2 \vdots u_Q v_Q
Output
Output Q lines.
The i-th line should contain the following value for graph G immediately after processing the i-th query.
- If a valid coloring is possible, the minimum possible number of vertices colored black.
- If no valid coloring is possible, -1.
Sample Input 1
4 4 1 2 2 3 3 4 1 3
Sample Output 1
1 1 2 -1
Immediately after processing queries 1, 2, 3, a valid coloring exists.
For example, immediately after processing the third query, vertices 1, 2, 3, 4 can be colored white, black, white, black, respectively. No valid coloring with fewer black vertices exists, so output 2.
Immediately after processing the fourth query, no valid coloring exists. Thus, output -1.
Sample Input 2
10 10 1 10 6 7 2 7 4 9 5 9 6 10 7 8 2 5 3 4 8 10
Sample Output 2
1 2 2 3 3 3 3 4 4 4