Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 問の問題が出題されるプログラミングコンテストがあります。 i = 1, 2, \ldots, N について、i 問目の配点は S_i です。
配点が X 以下である問題すべての配点の合計を出力してください。
制約
- 入力される値は全て整数
- 4 \leq N \leq 8
- 100 \leq S_i \leq 675
- 100 \leq X \leq 675
入力
入力は以下の形式で標準入力から与えられる。
N X S_1 S_2 \ldots S_N
出力
答えを出力せよ。
入力例 1
6 200 100 675 201 200 199 328
出力例 1
499
配点が 200 以下である問題は、1, 4, 5 問目の全 3 問であり、それらの配点の合計は S_1 + S_4 + S_5 = 100 + 200 + 199 = 499 です。
入力例 2
8 675 675 675 675 675 675 675 675 675
出力例 2
5400
入力例 3
8 674 675 675 675 675 675 675 675 675
出力例 3
0
Score : 100 points
Problem Statement
There is a programming contest with N problems. For each i = 1, 2, \ldots, N, the score for the i-th problem is S_i.
Print the total score for all problems with a score of X or less.
Constraints
- All input values are integers.
- 4 \leq N \leq 8
- 100 \leq S_i \leq 675
- 100 \leq X \leq 675
Input
The input is given from Standard Input in the following format:
N X S_1 S_2 \ldots S_N
Output
Print the answer.
Sample Input 1
6 200 100 675 201 200 199 328
Sample Output 1
499
Three problems have a score of 200 or less: the first, fourth, and fifth, for a total score of S_1 + S_4 + S_5 = 100 + 200 + 199 = 499.
Sample Input 2
8 675 675 675 675 675 675 675 675 675
Sample Output 2
5400
Sample Input 3
8 674 675 675 675 675 675 675 675 675
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
ある日、学校へ行くのに疲れてしまった高橋くんは、土曜日まであと何日あるかを知りたくなりました。
その日は平日で、曜日を英語で表すと S だったことが分かっています。その日より後の直近の土曜日は何日後かを求めてください。
なお、月曜日、火曜日、水曜日、木曜日、金曜日はそれぞれ英語で Monday, Tuesday, Wednesday, Thursday, Friday です。
制約
- S は
Monday,Tuesday,Wednesday,Thursday,Fridayのいずれかである
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
Wednesday
出力例 1
3
この日は水曜日なので、3 日後に土曜日になります。
入力例 2
Monday
出力例 2
5
Score : 100 points
Problem Statement
One day, tired from going to school, Takahashi wanted to know how many days there were until Saturday.
We know that the day was a weekday, and the name of the day of the week was S in English.
How many days were there until the first Saturday after that day (including Saturday but not the starting day)?
Constraints
- S is
Monday,Tuesday,Wednesday,Thursday, orFriday.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
Wednesday
Sample Output 1
3
It was Wednesday, so there were 3 days until the first Saturday after that day.
Sample Input 2
Monday
Sample Output 2
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
実数 X が小数点以下第 3 位まで与えられます。
実数 X を以下の条件を満たすように出力してください。
- 小数点以下の部分について、末尾に
0を付けない - 末尾に過剰な小数点を付けない
制約
- 0 \le X < 100
- X は小数点以下第 3 位まで与えられる
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを出力せよ。
入力例 1
1.012
出力例 1
1.012
1.012 はそのまま出力しても構いません。
入力例 2
12.340
出力例 2
12.34
12.340 を末尾に 0 を付けずに出力すると 12.34 となります。
入力例 3
99.900
出力例 3
99.9
99.900 を末尾に 0 を付けずに出力すると 99.9 となります。
入力例 4
0.000
出力例 4
0
0.000 を末尾に 0 や過剰な小数点を付けずに出力すると 0 となります。
Score : 150 points
Problem Statement
A real number X is given to the third decimal place.
Print the real number X under the following conditions.
- The decimal part must not have trailing
0s. - There must not be an unnecessary trailing decimal point.
Constraints
- 0 \le X < 100
- X is given to the third decimal place.
Input
The input is given from Standard Input in the following format:
X
Output
Output the answer.
Sample Input 1
1.012
Sample Output 1
1.012
1.012 can be printed as it is.
Sample Input 2
12.340
Sample Output 2
12.34
Printing 12.340 without the trailing 0 results in 12.34.
Sample Input 3
99.900
Sample Output 3
99.9
Printing 99.900 without the trailing 0s results in 99.9.
Sample Input 4
0.000
Sample Output 4
0
Printing 0.000 without trailing 0s or an unnecessary decimal point results in 0.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
年に 1 度の節分には年齢と同じ数の豆を食べます。高橋君はそれ以外に豆を食べることはありません。
高橋君は、今年 ( 0 年後 ) の節分の時点で N 歳です。
高橋君が今年以降 (今年を含む) で累計 K 個以上の豆を食べたことになるのは、最短で何年後の節分ですか?
なお、高橋君は不死身なので、高橋君が今年以降で累計 K 個以上の豆を食べたという状況は必ず訪れます。
制約
- 入力は全て整数
- 1 \le N,K \le 10^8
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
答えを出力せよ。
入力例 1
4 43
出力例 1
6
- 0 年後の節分に高橋君は 4 個の豆を食べ、今年以降で累計で 4 個の豆を食べます。
- 1 年後の節分に高橋君は 5 個の豆を食べ、今年以降で累計で 9 個の豆を食べます。
- 2 年後の節分に高橋君は 6 個の豆を食べ、今年以降で累計で 15 個の豆を食べます。
- 3 年後の節分に高橋君は 7 個の豆を食べ、今年以降で累計で 22 個の豆を食べます。
- 4 年後の節分に高橋君は 8 個の豆を食べ、今年以降で累計で 30 個の豆を食べます。
- 5 年後の節分に高橋君は 9 個の豆を食べ、今年以降で累計で 39 個の豆を食べます。
- 6 年後の節分に高橋君は 10 個の豆を食べ、今年以降で累計で 49 個の豆を食べます。
6 年後の節分の時点で累計 43 個以上の豆を食べたことになるので、 6 と出力してください。
入力例 2
100000000 100000000
出力例 2
0
今年のみで食べた豆の個数が K 個以上に達することもあります。
入力例 3
1234 12345678
出力例 3
3886
Score : 200 points
Problem Statement
At the annual Setsubun festival, one eats the same number of beans as their age. Takahashi-kun does not eat beans at any other time.
He is N years old at this year's Setsubun (0 years later).
At the earliest, how many years later will he have eaten a total of K or more beans from this year onward (including this year)?
He is immortal, so he will eventually have eaten a total of K or more beans from this year onward.
Constraints
- All input values are integers.
- 1 \le N,K \le 10^8
Input
The input is given from Standard Input in the following format:
N K
Output
Print the answer.
Sample Input 1
4 43
Sample Output 1
6
- At Setsubun 0 years later, Takahashi-kun eats 4 beans, totaling 4 beans from this year onward.
- At Setsubun 1 year later, he eats 5 beans, totaling 9 beans from this year onward.
- At Setsubun 2 years later, he eats 6 beans, totaling 15 beans from this year onward.
- At Setsubun 3 years later, he eats 7 beans, totaling 22 beans from this year onward.
- At Setsubun 4 years later, he eats 8 beans, totaling 30 beans from this year onward.
- At Setsubun 5 years later, he eats 9 beans, totaling 39 beans from this year onward.
- At Setsubun 6 years later, he eats 10 beans, totaling 49 beans from this year onward.
At Setsubun 6 years later, he will have eaten a total of 43 or more beans, so output 6.
Sample Input 2
100000000 100000000
Sample Output 2
0
The number of beans eaten this year alone may reach K or more.
Sample Input 3
1234 12345678
Sample Output 3
3886
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
アーティストの高砂君は、魚の骨をかたどったオブジェを作りました。
オブジェは N 本の肋骨と 1 本の脊椎からなります。 肋骨には 1 から N までの番号が付けられています。
高砂君は、以下の条件をすべて満たすように N+1 本の骨に 1 つずつ文字列を書こうと考えています。
- 脊椎に書く文字列の長さは N である。
- 肋骨 i = 1, \dots, N に対して、以下が成り立つ。
- 肋骨 i に書く文字列の長さは A_i である。
- 肋骨 i に書く文字列の B_i 文字目は、脊椎に書く文字列の i 文字目に一致する。
- N+1 本の骨に書く文字列はいずれも、S_1, \cdots, S_M のいずれかである(重複してもよい)。
S_1, \cdots, S_M は英小文字からなる文字列であり、互いに異なります。
j = 1, \cdots, M に対して、以下の質問に答えてください。
- 条件を満たす書き方のうち、脊椎に書く文字列が S_j であるものは存在しますか?
制約
- N は整数
- 1 \leq N \leq 10
- A_i, B_i は整数 (1 \leq i \leq N)
- 1 \leq B_i \leq A_i \leq 10 (1 \leq i \leq N)
- M は整数
- 1 \leq M \leq 200\,000
- S_j は英小文字からなる文字列 (1 \leq j \leq M)
- 1 \leq |S_j| \leq 10 (1 \leq j \leq M)
- S_1, \cdots, S_M は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 \vdots A_N B_N M S_1 \vdots S_M
出力
M 行出力せよ。
j 行目 (1 \leq j \leq M) には、条件を満たす書き方のうち脊椎に書く文字列が S_j のものが存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
5 5 3 5 2 4 1 5 1 3 2 8 retro chris itchy tuna crab rock cod ash
出力例 1
Yes Yes No No No No No No
肋骨 1,2,3,4,5 にそれぞれ chris, retro, tuna, retro, cod と書くことで、脊椎に retro を書いたときに条件を満たします。

retroの長さは 5 文字。- 各肋骨について、以下が成り立つ。
- 肋骨 1 に書かれる文字列
chrisの長さは 5 文字である。その 3 文字目はrであり、retroの 1 文字目に一致する。 - 肋骨 2 に書かれる文字列
retroの長さは 5 文字である。その 2 文字目はeであり、retroの 2 文字目に一致する。 - 肋骨 3 に書かれる文字列
tunaの長さは 4 文字である。その 1 文字目はtであり、retroの 3 文字目に一致する。 - 肋骨 4 に書かれる文字列
retroの長さは 5 文字である。その 1 文字目はrであり、retroの 4 文字目に一致する。 - 肋骨 5 に書かれる文字列
codの長さは 3 文字である。その 2 文字目はoであり、retroの 5 文字目に一致する。
- 肋骨 1 に書かれる文字列
肋骨 1,2,3,4,5 にそれぞれ itchy, chris, rock, itchy, ash と書くことで、脊椎に chris を書いたときに条件を満たします。

入力例 2
5 5 1 5 2 5 3 5 4 5 5 8 retro chris itchy tuna crab rock cod ash
出力例 2
Yes Yes Yes No No No No No
Score : 300 points
Problem Statement
Artist Takasago has created an object in the shape of a fish skeleton.
The object consists of N ribs and one spine. The ribs are numbered 1 through N.
He wants to write one string on each of the N+1 bones, satisfying all of the following conditions.
- The length of the string written on the spine is N.
- For each rib i = 1, \dots, N, the following hold.
- The length of the string written on rib i is A_i.
- The B_i-th character of the string written on rib i equals the i-th character of the string written on the spine.
- Each of the strings written on the N+1 bones is one of S_1, \cdots, S_M (duplicates allowed).
S_1, \cdots, S_M are strings consisting of lowercase English letters, and they are all distinct.
For each j = 1, \cdots, M, answer the following question.
- Among the ways to write strings satisfying the conditions, is there one where the string written on the spine is S_j?
Constraints
- N is an integer.
- 1 \leq N \leq 10
- A_i and B_i are integers. (1 \leq i \leq N)
- 1 \leq B_i \leq A_i \leq 10 (1 \leq i \leq N)
- M is an integer.
- 1 \leq M \leq 200\,000
- S_j is a string consisting of lowercase English letters. (1 \leq j \leq M)
- 1 \leq |S_j| \leq 10 (1 \leq j \leq M)
- S_1, \cdots, S_M are pairwise distinct.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 \vdots A_N B_N M S_1 \vdots S_M
Output
Output M lines.
The j-th line (1 \leq j \leq M) should contain Yes if there exists a way to write strings satisfying the conditions with S_j written on the spine, and No otherwise.
Sample Input 1
5 5 3 5 2 4 1 5 1 3 2 8 retro chris itchy tuna crab rock cod ash
Sample Output 1
Yes Yes No No No No No No
By writing chris, retro, tuna, retro, cod on ribs 1,2,3,4,5 respectively, the conditions are satisfied with retro written on the spine.

- The length of
retrois 5. - For each rib, the following hold.
- The string written on rib 1 is
chris, which has length 5. Its third character isr, which equals the first character ofretro. - The string written on rib 2 is
retro, which has length 5. Its second character ise, which equals the second character ofretro. - The string written on rib 3 is
tuna, which has length 4. Its first character ist, which equals the third character ofretro. - The string written on rib 4 is
retro, which has length 5. Its first character isr, which equals the fourth character ofretro. - The string written on rib 5 is
cod, which has length 3. Its second character iso, which equals the fifth character ofretro.
- The string written on rib 1 is
By writing itchy, chris, rock, itchy, ash on ribs 1,2,3,4,5 respectively, the conditions are satisfied with chris written on the spine.

Sample Input 2
5 5 1 5 2 5 3 5 4 5 5 8 retro chris itchy tuna crab rock cod ash
Sample Output 2
Yes Yes Yes No No No No No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
H 行 W 列の格子状に HW 枚のカードが並べられています。
i=1,\ldots,N について、上から A_i 行目、左から B_i 列目にあるカードには数 i が書かれており、それ以外の HW-N 枚のカードには何も書かれていません。
これらのカードに対し、以下の 2 種類の操作を可能な限り繰り返します。
- 数の書かれたカードを含まない行が存在するとき、その行のカードを全て取り除き、残りのカードを上へ詰める
- 数の書かれたカードを含まない列が存在するとき、その列のカードを全て取り除き、残りのカードを左へ詰める
操作が終了したとき、数が書かれたカードがそれぞれどこにあるか求めてください。なお、答えは操作の仕方に依らず一意に定まることが証明されます。
制約
- 1 \leq H,W \leq 10^9
- 1 \leq N \leq \min(10^5,HW)
- 1 \leq A_i \leq H
- 1 \leq B_i \leq W
- (A_i,B_i) は相異なる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
H W N A_1 B_1 \vdots A_N B_N
出力
N 行出力せよ。
操作終了後に数 i が書かれたカードが上から C_i 行目、左から D_i 列目に存在するとき、i 行目には C_i,D_i をこの順に空白区切りで出力せよ。
入力例 1
4 5 2 3 2 2 5
出力例 1
2 1 1 2
何も書かれていないカードを * で表すことにします。最初、カードの配置は以下の通りです。
***** ****2 *1*** *****
操作終了後、カードの配置は以下の通りになります。
*2 1*
1 が書かれたカードは上から 2 行目、左から 1 列目にあり、2 が書かれたカードは上から 1 行目、左から 2 列目にあります。
入力例 2
1000000000 1000000000 10 1 1 10 10 100 100 1000 1000 10000 10000 100000 100000 1000000 1000000 10000000 10000000 100000000 100000000 1000000000 1000000000
出力例 2
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
Score : 300 points
Problem Statement
We have HW cards arranged in a matrix with H rows and W columns.
For each i=1, \ldots, N, the card at the A_i-th row from the top and B_i-th column from the left has a number i written on it. The other HW-N cards have nothing written on them.
We will repeat the following two kinds of operations on these cards as long as we can.
- If there is a row without a numbered card, remove all the cards in that row and fill the gap by shifting the remaining cards upward.
- If there is a column without a numbered card, remove all the cards in that column and fill the gap by shifting the remaining cards to the left.
Find the position of each numbered card after the end of the process above. It can be proved that these positions are uniquely determined without depending on the order in which the operations are done.
Constraints
- 1 \leq H,W \leq 10^9
- 1 \leq N \leq \min(10^5,HW)
- 1 \leq A_i \leq H
- 1 \leq B_i \leq W
- All pairs (A_i,B_i) are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W N A_1 B_1 \vdots A_N B_N
Output
Print N lines.
If, after the end of the process, the card with the number i is at the C_i-th row from the top and D_i-th column from the left, the i-th line should contain C_i and D_i in this order with a space between them.
Sample Input 1
4 5 2 3 2 2 5
Sample Output 1
2 1 1 2
Let * represent a card with nothing written on it. The initial arrangement of cards is:
***** ****2 *1*** *****
After the end of the process, they will be arranged as follows:
*2 1*
Here, the card with 1 is at the 2-nd row from the top and 1-st column from the left, and the card with 2 is at the 1-st row from the top and 2-nd column from the left.
Sample Input 2
1000000000 1000000000 10 1 1 10 10 100 100 1000 1000 10000 10000 100000 100000 1000000 1000000 10000000 10000000 100000000 100000000 1000000000 1000000000
Sample Output 2
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
(1, 2, \dots, N) を並び替えて得られる数列 P であって以下の条件を満たすもののうち、辞書順で最小のものを求めてください。
- i = 1, \dots, M に対し、P において A_i は B_i よりも先に現れる。
ただし、そのような P が存在しない場合は -1 と出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
4 3 2 1 3 4 2 4
出力例 1
2 1 3 4
条件を満たす P は (2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1) の 5 つです。これらのうち辞書順で最小のものは (2, 1, 3, 4) です。
入力例 2
2 3 1 2 1 2 2 1
出力例 2
-1
条件を満たす P は存在しません。
Score : 400 points
Problem Statement
Among the sequences P that are permutations of (1, 2, \dots, N) and satisfy the condition below, find the lexicographically smallest sequence.
- For each i = 1, \dots, M, A_i appears earlier than B_i in P.
If there is no such P, print -1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Print the answer.
Sample Input 1
4 3 2 1 3 4 2 4
Sample Output 1
2 1 3 4
The following five permutations P satisfy the condition: (2, 1, 3, 4), (2, 3, 1, 4), (2, 3, 4, 1), (3, 2, 1, 4), (3, 2, 4, 1). The lexicographically smallest among them is (2, 1, 3, 4).
Sample Input 2
2 3 1 2 1 2 2 1
Sample Output 2
-1
No permutations P satisfy the condition.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の重み付き無向連結グラフ G が与えられます。G には自己ループや多重辺が含まれている可能性があります。
頂点には頂点 1, 頂点 2, \dots, 頂点 N と番号がついています。
辺には辺 1, 辺 2, \dots, 辺 M と番号がついています。辺 i は頂点 a_i と頂点 b_i を結ぶ重み c_i の辺です。ここで、1 \leq i \lt j \leq M を満たすすべての整数の組 (i, j) について c_i \neq c_j が成り立ちます。
以下で説明される Q 個のクエリに答えてください。
i 番目のクエリでは整数の組 (u_i, v_i, w_i) が与えられます。ここで、1 \leq j \leq M を満たすすべての整数 j について w_i \neq c_j が成り立ちます。
頂点 u_i と頂点 v_i を結ぶ重み w_i の無向辺を e_i として、G に e_i を追加してできるグラフ G_i を考えます。
このとき G_i の最小全域木 T_i は一意に定まることが証明できますが、T_i に e_i は含まれるでしょうか?答えを Yes あるいは No で出力してください。
ここで、クエリの前後で G は変化しないことに注意してください。言い換えると、クエリ i で G に e_i を追加したグラフを考えたとしても、他のクエリで出てくる G に e_i が追加されていることはありません。
最小全域木とは?
G の 全域木 とは、G に含まれるすべての頂点と G に含まれる辺の一部からなる木のことを言います。G の 最小全域木 とは、G の全域木の中で辺の重みの和が最小である木のことを言います。
制約
- 2 \leq N \leq 2 \times 10^5
- N - 1 \leq M \leq 2 \times 10^5
- 1 \leq a_i \leq N (1 \leq i \leq M)
- 1 \leq b_i \leq N (1 \leq i \leq M)
- 1 \leq c_i \leq 10^9 (1 \leq i \leq M)
- c_i \neq c_j (1 \leq i \lt j \leq M)
- グラフ G は連結である。
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq u_i \leq N (1 \leq i \leq Q)
- 1 \leq v_i \leq N (1 \leq i \leq Q)
- 1 \leq w_i \leq 10^9 (1 \leq i \leq Q)
- w_i \neq c_j (1 \leq i \leq Q, 1 \leq j \leq M)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M Q a_1 b_1 c_1 a_2 b_2 c_2 \vdots a_M b_M c_M u_1 v_1 w_1 u_2 v_2 w_2 \vdots u_Q v_Q w_Q
出力
Q 行出力せよ。i 行目ではクエリ i への答えを Yes または No で出力せよ。
入力例 1
5 6 3 1 2 2 2 3 3 1 3 6 2 4 5 4 5 9 3 5 8 1 3 1 3 4 7 3 5 7
出力例 1
Yes No Yes
以下では頂点 u と頂点 v を結ぶ重み w の無向辺を (u,v,w) と表します。 G を図に表したものを以下に挙げます。

たとえばクエリ 1 では G に e_1 = (1,3,1) を追加したグラフ G_1 を考えます。G_1 の最小全域木 T_1 の辺集合は \lbrace (1,2,2),(1,3,1),(2,4,5),(3,5,8) \rbrace であり e_1 を含みます。よって Yes を出力します。
入力例 2
2 3 2 1 2 100 1 2 1000000000 1 1 1 1 2 2 1 1 5
出力例 2
Yes No
Score : 500 points
Problem Statement
Given is a weighted undirected connected graph G with N vertices and M edges, which may contain self-loops and multi-edges.
The vertices are labeled as Vertex 1, Vertex 2, \dots, Vertex N.
The edges are labeled as Edge 1, Edge 2, \ldots, Edge M. Edge i connects Vertex a_i and Vertex b_i and has a weight of c_i. Here, for every pair of integers (i, j) such that 1 \leq i \lt j \leq M, c_i \neq c_j holds.
Process the Q queries explained below.
The i-th query gives a triple of integers (u_i, v_i, w_i). Here, for every integer j such that 1 \leq j \leq M, w_i \neq c_j holds.
Let e_i be an undirected edge that connects Vertex u_i and Vertex v_i and has a weight of w_i. Consider the graph G_i obtained by adding e_i to G.
It can be proved that the minimum spanning tree T_i of G_i is uniquely determined. Does T_i contain e_i? Print the answer as Yes or No.
Note that the queries do not change T. In other words, even though Query i considers the graph obtained by adding e_i to G, the G in other queries does not have e_i.
What is minimum spanning tree?
The spanning tree of G is a tree with all of the vertices in G and some of the edges in G.The minimum spanning tree of G is the tree with the minimum total weight of edges among the spanning trees of G.
Constraints
- 2 \leq N \leq 2 \times 10^5
- N - 1 \leq M \leq 2 \times 10^5
- 1 \leq a_i \leq N (1 \leq i \leq M)
- 1 \leq b_i \leq N (1 \leq i \leq M)
- 1 \leq c_i \leq 10^9 (1 \leq i \leq M)
- c_i \neq c_j (1 \leq i \lt j \leq M)
- The graph G is connected.
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq u_i \leq N (1 \leq i \leq Q)
- 1 \leq v_i \leq N (1 \leq i \leq Q)
- 1 \leq w_i \leq 10^9 (1 \leq i \leq Q)
- w_i \neq c_j (1 \leq i \leq Q, 1 \leq j \leq M)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M Q a_1 b_1 c_1 a_2 b_2 c_2 \vdots a_M b_M c_M u_1 v_1 w_1 u_2 v_2 w_2 \vdots u_Q v_Q w_Q
Output
Print Q lines. The i-th line should contain the answer to Query i: Yes or No.
Sample Input 1
5 6 3 1 2 2 2 3 3 1 3 6 2 4 5 4 5 9 3 5 8 1 3 1 3 4 7 3 5 7
Sample Output 1
Yes No Yes
Below, let (u,v,w) denote an undirected edge that connects Vertex u and Vertex v and has the weight of w. Here is an illustration of G:

For example, Query 1 considers the graph G_1 obtained by adding e_1 = (1,3,1) to G. The minimum spanning tree T_1 of G_1 has the edge set \lbrace (1,2,2),(1,3,1),(2,4,5),(3,5,8) \rbrace, which contains e_1, so Yes should be printed.
Sample Input 2
2 3 2 1 2 100 1 2 1000000000 1 1 1 1 2 2 1 1 5
Sample Output 2
Yes No
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