実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
英小文字および .
からなる文字列 S が与えられます。
S から .
を全て削除した文字列を求めてください。
制約
- S は英小文字および
.
からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S から .
を全て削除した文字列を出力せよ。
入力例 1
.v.
出力例 1
v
.v.
から .
を全て削除すると v
になります。したがって、 v
を出力してください。
入力例 2
chokudai
出力例 2
chokudai
S に .
が含まれない場合もあります。
入力例 3
...
出力例 3
S の文字が全て .
である場合もあります。
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters and .
.
Find the string obtained by removing all .
from S.
Constraints
- S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters and
.
.
Input
The input is given from Standard Input in the following format:
S
Output
Print the string obtained by removing all .
from S.
Sample Input 1
.v.
Sample Output 1
v
Removing all .
from .v.
yields v
, so print v
.
Sample Input 2
chokudai
Sample Output 2
chokudai
There are cases where S does not contain .
.
Sample Input 3
...
Sample Output 3
There are also cases where all characters in S are .
.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
ナオヒロ君はモンスターを飼っています。モンスターの現在の体力は H です。
また、ナオヒロ君は N 種類の傷薬を持っています。傷薬は効き目の弱い順に 1 から N までの番号がついています。
傷薬 n をモンスターに与えると、モンスターの体力が P_n 増加します。ここで、P_1 \lt P_2 \lt \dots \lt P_N が成り立ちます。
ナオヒロ君は傷薬を 1 つモンスターに与えることで、モンスターの体力を X 以上にしたいです。
目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力してください。(制約下においてそのような傷薬が存在することが保証されています。)
制約
- 2 \leq N \leq 100
- 1 \leq H \lt X \leq 999
- 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H X P_1 P_2 \dots P_N
出力
目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力せよ。
入力例 1
3 100 200 50 200 999
出力例 1
2
それぞれの傷薬をモンスターに 1 つ与えたときのモンスターの体力の変化は以下の通りです。
- 傷薬 1 をモンスターに与えるとモンスターの体力は 100 + 50 = 150 になります。
- 傷薬 2 をモンスターに与えるとモンスターの体力は 100 + 200 = 300 になります。
- 傷薬 3 をモンスターに与えるとモンスターの体力は 100 + 999 = 1099 になります。
与えた後に体力が X = 200 以上になっている傷薬は、傷薬 2 と傷薬 3 です。このうち最も効き目の弱い傷薬である傷薬 2 が答えになります。
入力例 2
2 10 21 10 999
出力例 2
2
入力例 3
10 500 999 38 420 490 585 613 614 760 926 945 999
出力例 3
4
Score : 100 points
Problem Statement
Naohiro has a monster. The monster's current health is H.
He also has N kinds of potions, numbered from 1 to N in ascending order of effectiveness.
If you give the monster potion n, its health will increase by P_n. Here, P_1 \lt P_2 \lt \dots \lt P_N.
He wants to increase the monster's health to X or above by giving it one of the potions.
Print the number of the least effective potion that can achieve the purpose. (The constraints guarantee that such a potion exists.)
Constraints
- 2 \leq N \leq 100
- 1 \leq H \lt X \leq 999
- 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H X P_1 P_2 \dots P_N
Output
Print the number of the least effective potion that can achieve the purpose.
Sample Input 1
3 100 200 50 200 999
Sample Output 1
2
Below is the change in the monster's health when one of the potions is given to the monster.
- If potion 1 is given, the monster's health becomes 100 + 50 = 150.
- If potion 2 is given, the monster's health becomes 100 + 200 = 300.
- If potion 3 is given, the monster's health becomes 100 + 999 = 1099.
The potions that increase the monster's health to at least X = 200 are potions 2 and 3. The answer is the least effective of them, which is potion 2.
Sample Input 2
2 10 21 10 999
Sample Output 2
2
Sample Input 3
10 500 999 38 420 490 585 613 614 760 926 945 999
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 人の競技プログラマーがいます。順に 人 1, 人 2, \dots, 人 N と呼びます。
競技プログラマーの間には 強さ と呼ばれる関係性があり、相異なる 2 人組 (人 X, 人 Y) 全てに対して 「人 X は人 Y より強い」または「人 Y は人 X より強い」のどちらか一方が成り立ちます。
強さ は 推移律 が成り立ちます。言い換えると、相異なる 3 人組 (人 X, 人 Y, 人 Z) 全てに対して次の条件が成り立ちます。
- 人 X が人 Y よりも強く、かつ人 Y が人 Z よりも強いとき、人 X は人 Z よりも強い。
人 X が自分以外のどの人 Y に対しても「人 X は人 Y より強い」という関係が成り立つ時、人 X を 最強のプログラマー と呼びます。(上記の制約下においてそのような人がちょうど 1 人存在することが証明できます)
あなたは M 個の強さに関する情報を知っています。i 個目の情報は「人 A_i は人 B_i より強い」という情報です。
あなたは情報を元に N 人の中から最強のプログラマーを特定することができますか?
最強のプログラマーを特定できる場合はその人の番号を出力してください。特定できない場合、つまり最強のプログラマーとしてあり得る人が複数人いる場合は -1
を出力してください。
制約
- 2 \leq N \leq 50
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- i \neq j ならば (A_i, B_i) \neq (A_j, B_j)
- 全ての情報が正しくなるように、全ての相異なる 2 人組にどちらが強いかを割り当てる方法が少なくとも 1 通り存在する
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
最強のプログラマーを特定できる場合はその人の番号を出力せよ。特定できない場合は -1
を出力せよ。
入力例 1
3 2 1 2 2 3
出力例 1
1
あなたは「人 1 は人 2 より強い」「人 2 は人 3 より強い」という情報が分かっています。
推移律から「人 1 は人 3 より強い」という情報もわかるので、人 1 は最強のプログラマーです。
入力例 2
3 2 1 3 2 3
出力例 2
-1
最強のプログラマーである可能性がある人は人 1 と人 2 です。最強のプログラマーを一意に特定できないので -1
を出力してください。
入力例 3
6 6 1 6 6 5 6 2 2 3 4 3 4 2
出力例 3
-1
Score : 300 points
Problem Statement
There are N competitive programmers numbered person 1, person 2, \ldots, and person N.
There is a relation called superiority between the programmers. For all pairs of distinct programmers (person X, person Y), exactly one of the following two relations holds: "person X is stronger than person Y" or "person Y is stronger than person X."
The superiority is transitive. In other words, for all triplets of distinct programmers (person X, person Y, person Z), it holds that:
- if person X is stronger than person Y and person Y is stronger than person Z, then person X is stronger than person Z.
A person X is said to be the strongest programmer if person X is stronger than person Y for all people Y other than person X. (Under the constraints above, we can prove that there is always exactly one such person.)
You have M pieces of information on their superiority. The i-th of them is that "person A_i is stronger than person B_i."
Can you determine the strongest programmer among the N based on the information?
If you can, print the person's number. Otherwise, that is, if there are multiple possible strongest programmers, print -1
.
Constraints
- 2 \leq N \leq 50
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- If i \neq j, then (A_i, B_i) \neq (A_j, B_j).
- There is at least one way to determine superiorities for all pairs of distinct programmers, that is consistent with the given information.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
If you can uniquely determine the strongest programmer, print the person's number; otherwise, print -1
.
Sample Input 1
3 2 1 2 2 3
Sample Output 1
1
You have two pieces of information: "person 1 is stronger than person 2" and "person 2 is stronger than person 3."
By the transitivity, you can also infer that "person 1 is stronger than person 3," so person 1 is the strongest programmer.
Sample Input 2
3 2 1 3 2 3
Sample Output 2
-1
Both person 1 and person 2 may be the strongest programmer. Since you cannot uniquely determine which is the strongest, you should print -1
.
Sample Input 3
6 6 1 6 6 5 6 2 2 3 4 3 4 2
Sample Output 3
-1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英大文字と英小文字からなる文字列のうち、以下の条件を全て満たすものを素晴らしい文字列ということとします。
- 英大文字が文字列の中に現れる。
- 英小文字が文字列の中に現れる。
- 全ての文字が相異なる。
例えば、AtCoder
や Aa
は素晴らしい文字列ですが、atcoder
や Perfect
は素晴らしい文字列ではありません。
文字列 S が与えられるので、S が素晴らしい文字列か判定してください。
制約
- 1 \le |S| \le 100
- S は英大文字と英小文字からなる文字列である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が素晴らしい文字列ならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
AtCoder
出力例 1
Yes
AtCoder
は、英大文字が含まれ、英小文字も含まれ、かつ全ての文字が相異なるため素晴らしい文字列です。
入力例 2
Aa
出力例 2
Yes
A
と a
は違う文字であることに注意してください。この文字列は素晴らしい文字列です。
入力例 3
atcoder
出力例 3
No
英大文字が含まれていないため、素晴らしい文字列ではありません。
入力例 4
Perfect
出力例 4
No
2 文字目と 5 文字目が等しいため、素晴らしい文字列ではありません。
Score : 200 points
Problem Statement
Let us call a string consisting of uppercase and lowercase English alphabets a wonderful string if all of the following conditions are satisfied:
- The string contains an uppercase English alphabet.
- The string contains a lowercase English alphabet.
- All characters in the string are pairwise distinct.
For example, AtCoder
and Aa
are wonderful strings, while atcoder
and Perfect
are not.
Given a string S, determine if S is a wonderful string.
Constraints
- 1 \le |S| \le 100
- S is a string consisting of uppercase and lowercase English alphabets.
Input
Input is given from Standard Input in the following format:
S
Output
If S is a wonderful string, print Yes
; otherwise, print No
.
Sample Input 1
AtCoder
Sample Output 1
Yes
AtCoder
is a wonderful string because it contains an uppercase English alphabet, a lowercase English alphabet, and all characters in the string are pairwise distinct.
Sample Input 2
Aa
Sample Output 2
Yes
Note that A
and a
are different characters. This string is a wonderful string.
Sample Input 3
atcoder
Sample Output 3
No
It is not a wonderful string because it does not contain an uppercase English alphabet.
Sample Input 4
Perfect
Sample Output 4
No
It is not a wonderful string because the 2-nd and the 5-th characters are the same.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
WAtCoder には N 人のユーザーがおり、1 から N までの番号がつけられています。 また、M 個のコンテストページがあり、1 から M までの番号がつけられています。 はじめ、すべてのユーザはどのコンテストページの閲覧権限も持っていません。
Q 個のクエリが与えられるので、順に処理してください。クエリは 3 種類あり、以下のいずれかの形式で与えられます。
1 X Y
: ユーザ X にコンテストページ Y の閲覧権限を付与する。2 X
: ユーザ X にすべてのコンテストページの閲覧権限を付与する。3 X Y
: ユーザ X がコンテストページ Y を閲覧できるかを答える。
クエリの中で、あるユーザがすでに閲覧権限を持っているコンテストページについて、重ねて閲覧権限を付与されることもあります。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq X \leq N
- 1 \leq Y \leq M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
各クエリ \mathrm{query}_i は以下の 3 種類のいずれかの形式で与えられる。
1 X Y
2 X
3 X Y
出力
3 種類目のクエリのそれぞれについて、ユーザ X がコンテストページ Y を閲覧できるならば Yes
を、そうでなければ No
を改行区切りで出力せよ。
入力例 1
2 3 5 1 1 2 3 1 1 3 1 2 2 2 3 2 3
出力例 1
No Yes Yes
- 1 つ目のクエリで、ユーザ 1 にコンテストページ 2 の閲覧権限を付与します。
- 2 つ目のクエリの時点で、ユーザ 1 が閲覧できるコンテストページは 2 のみです。コンテストページ 1 の閲覧権限を持っていないので、
No
を出力します。 - 3 つ目のクエリの時点で、ユーザ 1 はコンテストページ 2 の閲覧権限を持っているので、
Yes
を出力します。 - 4 つ目のクエリで、ユーザ 2 にすべてのコンテストページの閲覧権限を付与します。
- 5 つ目のクエリの時点で、ユーザ 2 が閲覧できるコンテストページは 1,2,3 です。コンテストページ 3 の閲覧権限を持っているので、
Yes
を出力します。
入力例 2
5 5 10 2 2 3 4 4 1 1 1 1 4 1 1 4 2 1 4 4 1 2 4 3 3 2 3 5 4 3 2 1
出力例 2
No No No Yes
Score : 300 points
Problem Statement
There are N users on WAtCoder, numbered from 1 to N, and M contest pages, numbered from 1 to M. Initially, no user has view permission for any contest page.
You are given Q queries to process in order. Each query is of one of the following three types:
1 X Y
: Grant user X view permission for contest page Y.2 X
: Grant user X view permission for all contest pages.3 X Y
: Answer whether user X can view contest page Y.
It is possible for a user to be granted permission for the same contest page multiple times.
Constraints
- 1 \le N \le 2\times 10^5
- 1 \le M \le 2\times 10^5
- 1 \le Q \le 2\times 10^5
- 1 \le X \le N
- 1 \le Y \le M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Each \mathrm{query}_i is in one of the following formats:
1 X Y
2 X
3 X Y
Output
For each query of the third type, print Yes
if user X can view contest page Y, otherwise print No
, each on its own line.
Sample Input 1
2 3 5 1 1 2 3 1 1 3 1 2 2 2 3 2 3
Sample Output 1
No Yes Yes
- In the first query, user 1 is granted permission to view contest page 2.
- At the second query, user 1 can view only page 2; they cannot view page 1, so print
No
. - At the third query, user 1 can view page 2, so print
Yes
. - In the fourth query, user 2 is granted permission to view all pages.
- At the fifth query, user 2 can view pages 1,2,3; they can view page 3, so print
Yes
.
Sample Input 2
5 5 10 2 2 3 4 4 1 1 1 1 4 1 1 4 2 1 4 4 1 2 4 3 3 2 3 5 4 3 2 1
Sample Output 2
No No No Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
あなたはアメーバの観察記録をつけました。
最初 1 匹のアメーバがおり、番号は 1 です。
観察記録は時系列順に N 個あり、i 番目の観察記録は「番号 A_i のアメーバが分裂して消滅し、新たに 2 匹のアメーバが生まれ、それらにそれぞれ 2i,2i+1 と番号をつけた」というものです。
このとき、アメーバ A_i を アメーバ 2i,2i+1 の親と呼びます。
各 k=1,\ldots,2N+1 について、アメーバ k から何代親を遡るとアメーバ 1 になるか求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- 観察記録は矛盾していない。すなわち
- 1\leq A_i \leq 2i-1
- A_i は相異なる整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
2N+1 行出力せよ。k 行目にはアメーバ k から何代親を遡るとアメーバ 1 になるかを出力せよ。
入力例 1
2 1 2
出力例 1
0 1 1 2 2
アメーバ 1 からアメーバ 2,3 が生まれ、アメーバ 2 からアメーバ 4,5 が生まれました。
- アメーバ 1 は 0 代遡るとアメーバ 1 になります。
- アメーバ 2 は 1 代遡るとアメーバ 1 になります。
- アメーバ 3 は 1 代遡るとアメーバ 1 になります。
- アメーバ 4 は 1 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。
- アメーバ 5 は 1 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。
入力例 2
4 1 3 5 2
出力例 2
0 1 1 2 2 3 3 2 2
Score : 300 points
Problem Statement
You observed amoebae and kept some records.
Initially, there was one amoeba, numbered 1.
You made N records. According to the i-th record, the amoeba numbered A_i disappeared by dividing itself into two new amoebae, which were then numbered 2i and 2i+1.
Here, amoeba A_i is said to be the parent of amoebae 2i and 2i+1.
For each k=1,\ldots,2N+1, how many generations away is amoeba k from amoeba 1?
Constraints
- 1 \leq N \leq 2\times 10^5
- The records are consistent. That is:
- 1\leq A_i \leq 2i-1.
- A_i are distinct integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print 2N+1 lines. The k-th line should contain the generation distance between amoeba 1 and amoeba k.
Sample Input 1
2 1 2
Sample Output 1
0 1 1 2 2
From amoeba 1, amoebae 2 and 3 were born. From amoeba 2, amoebae 4 and 5 were born.
- Amoeba 1 is zero generations away from amoeba 1.
- Amoeba 2 is one generation away from amoeba 1.
- Amoeba 3 is one generation away from amoeba 1.
- Amoeba 4 is one generation away from amoeba 2, and two generations away from amoeba 1.
- Amoeba 5 is one generation away from amoeba 2, and two generations away from amoeba 1.
Sample Input 2
4 1 3 5 2
Sample Output 2
0 1 1 2 2 3 3 2 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N \times M のグリッドがあり、この上にプレイヤーがいます。
このグリッドの上から i 行目、左から j 列目をマス (i,j) と書きます。
このグリッドの各マスは 氷 か 岩 であり、その情報は N 個の長さ M の文字列 S_1,S_2,\dots,S_N として与えられます。
- もし S_i の j 文字目が
.
なら、マス (i,j) は 氷 である。 - もし S_i の j 文字目が
#
なら、マス (i,j) は 岩 である。
なお、このグリッドの外周 ( 1 行目、 N 行目、 1 列目、 M 列目の全てのマス ) は 岩 です。
最初、プレイヤーはマス (2,2) の上で停止しています。このマスは 氷 です。
プレイヤーは以下の行動を 0 度以上何度でも行うことができます。
- まず、プレイヤーは上下左右の移動方向を指定する。
- その後、プレイヤーは岩のマスにぶつかるまでその方向に移動する。厳密には以下の通りである。
- もし移動方向に隣接するマスが 氷 なら、そのマスに移動し、同じ方向に移動を継続する。
- もし移動方向に隣接するマスが 岩 なら、今いるマスに留まり、移動を終了する。
プレイヤーが触れる (通過または上で停止する) ことができる 氷 の数を求めてください。
制約
- 3 \le N,M \le 200
- S_i は
#
と.
からなる長さ M の文字列 - i=1 または i=N または j=1 または j=M であるとき、マス (i,j) は 岩
- マス (2,2) は 氷
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
答えを整数として出力せよ。
入力例 1
6 6 ###### #....# #.#..# #..#.# #....# ######
出力例 1
12
例えばマス (5,5) には以下のように移動することで上で停止することができます。
- (2,2) \rightarrow (5,2) \rightarrow (5,5)
例えばマス (2,4) には以下のように移動することで通過することができます。
- (2,2) \rightarrow (2,5) の移動中に (2,4) を通過する。
例えばマス (3,4) は通過することも上で停止することもできません。
入力例 2
21 25 ######################### #..............###...#### #..............#..#...### #........###...#...#...## #........#..#..#........# #...##...#..#..#...#....# #..#..#..###...#..#.....# #..#..#..#..#..###......# #..####..#..#...........# #..#..#..###............# #..#..#.................# #........##.............# #.......#..#............# #..........#....#.......# #........###...##....#..# #..........#..#.#...##..# #.......#..#....#..#.#..# ##.......##.....#....#..# ###.............#....#..# ####.................#..# #########################
出力例 2
215
Score : 400 points
Problem Statement
There is an N \times M grid and a player standing on it.
Let (i,j) denote the square at the i-th row from the top and j-th column from the left of this grid.
Each square of this grid is ice or rock, which is represented by N strings S_1,S_2,\dots,S_N of length M as follows:
- if the j-th character of S_i is
.
, square (i,j) is ice; - if the j-th character of S_i is
#
, square (i,j) is rock.
The outer periphery of this grid (all squares in the 1-st row, N-th row, 1-st column, M-th column) is rock.
Initially, the player rests on the square (2,2), which is ice.
The player can make the following move zero or more times.
- First, specify the direction of movement: up, down, left, or right.
- Then, keep moving in that direction until the player bumps against a rock. Formally, keep doing the following:
- if the next square in the direction of movement is ice, go to that square and keep moving;
- if the next square in the direction of movement is rock, stay in the current square and stop moving.
Find the number of ice squares the player can touch (pass or rest on).
Constraints
- 3 \le N,M \le 200
- S_i is a string of length M consisting of
#
and.
. - Square (i, j) is rock if i=1, i=N, j=1, or j=M.
- Square (2,2) is ice.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print the answer as an integer.
Sample Input 1
6 6 ###### #....# #.#..# #..#.# #....# ######
Sample Output 1
12
For instance, the player can rest on (5,5) by moving as follows:
- (2,2) \rightarrow (5,2) \rightarrow (5,5).
The player can pass (2,4) by moving as follows:
- (2,2) \rightarrow (2,5), passing (2,4) in the process.
The player cannot pass or rest on (3,4).
Sample Input 2
21 25 ######################### #..............###...#### #..............#..#...### #........###...#...#...## #........#..#..#........# #...##...#..#..#...#....# #..#..#..###...#..#.....# #..#..#..#..#..###......# #..####..#..#...........# #..#..#..###............# #..#..#.................# #........##.............# #.......#..#............# #..........#....#.......# #........###...##....#..# #..........#..#.#...##..# #.......#..#....#..#.#..# ##.......##.....#....#..# ###.............#....#..# ####.................#..# #########################
Sample Output 2
215
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
ある国には N 個の都市と M 個の発電所があります。これらを総称して地点と呼びます。
地点には 1,2,\dots,N+M の番号がつけられており、そのうち都市は地点 1,2,\dots,N で発電所は地点 N+1,N+2,\dots,N+M です。
この国には電線が E 本あり、電線 i ( 1 \le i \le E ) は地点 U_i と地点 V_i を双方向に結びます。
また、ある都市に 電気が通っている とは、ある都市から電線をいくつか辿って少なくともひとつの発電所に辿り着くことができる状態を言います。
今、 Q 個のイベントが起こります。そのうち i ( 1 \le i \le Q ) 番目のイベントでは電線 X_i が切れ、その電線を辿ることができなくなります。一度切れた電線は、その後のイベントにおいても切れたままです。
全てのイベントについて、そのイベントが終わった直後に電気が通っている都市の数を求めてください。
制約
- 入力は全て整数
- 1 \le N,M
- N+M \le 2 \times 10^5
- 1 \le Q \le E \le 5 \times 10^5
- 1 \le U_i < V_i \le N+M
- i \neq j ならば、 U_i \neq U_j または V_i \neq V_j
- 1 \le X_i \le E
- X_i は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N M E U_1 V_1 U_2 V_2 \vdots U_E V_E Q X_1 X_2 \vdots X_Q
出力
Q 行出力せよ。
そのうち i ( 1 \le i \le Q ) 行目には i 番目のイベントが終わった直後に電気が通っている都市の数を出力せよ。
入力例 1
5 5 10 2 3 4 10 5 10 6 9 2 9 4 8 1 7 3 6 8 10 1 8 6 3 5 8 10 2 7
出力例 1
4 4 2 2 2 1
はじめ、全ての都市に電気が通っています。
- 1 番目のイベントによって地点 5 と地点 10 を結ぶ電線 3 が切れます。
- これにより、都市 5 に電気が通らなくなり、電気が通っている都市の数は 4 となります。
- 2 番目のイベントによって地点 2 と地点 9 を結ぶ電線 5 が切れます。
- 3 番目のイベントによって地点 3 と地点 6 を結ぶ電線 8 が切れます。
- これにより、都市 2,3 に電気が通らなくなり、電気が通っている都市の数は 2 となります。
- 4 番目のイベントによって地点 1 と地点 8 を結ぶ電線 10 が切れます。
- 5 番目のイベントによって地点 4 と地点 10 を結ぶ電線 2 が切れます。
- 6 番目のイベントによって地点 1 と地点 7 を結ぶ電線 7 が切れます。
- これにより、都市 1 に電気が通らなくなり、電気が通っている都市の数は 1 となります。
Score : 500 points
Problem Statement
A country has N cities and M power plants, which we collectively call places.
The places are numbered 1,2,\dots,N+M, among which Places 1,2,\dots,N are the cities and Places N+1,N+2,\dots,N+M are the power plants.
This country has E power lines. Power Line i (1 \le i \le E) connects Place U_i and Place V_i bidirectionally.
A city is said to be electrified if one can reach at least one of the power plants from the city using some power lines.
Now, Q events will happen. In the i-th (1 \le i \le Q) event, Power Line X_i breaks, making it unusable. Once a power line breaks, it remains broken in the succeeding events.
Find the number of electrified cities right after each events.
Constraints
- All values in input are integers.
- 1 \le N,M
- N+M \le 2 \times 10^5
- 1 \le Q \le E \le 5 \times 10^5
- 1 \le U_i < V_i \le N+M
- If i \neq j, then U_i \neq U_j or V_i \neq V_j.
- 1 \le X_i \le E
- X_i are distinct.
Input
Input is given from Standard Input in the following format:
N M E U_1 V_1 U_2 V_2 \vdots U_E V_E Q X_1 X_2 \vdots X_Q
Output
Print Q lines.
The i-th line should contain the number of electrified cities right after the i-th event.
Sample Input 1
5 5 10 2 3 4 10 5 10 6 9 2 9 4 8 1 7 3 6 8 10 1 8 6 3 5 8 10 2 7
Sample Output 1
4 4 2 2 2 1
Initially, all cities are electrified.
- The 1-st event breaks Power Line 3 that connects Point 5 and Point 10.
- Now City 5 is no longer electrified, while 4 cities remain electrified.
- The 2-nd event breaks Power Line 5 that connects Point 2 and Point 9.
- The 3-rd event breaks Power Line 8 that connects Point 3 and Point 6.
- Now Cities 2 and 3 are no longer electrified, while 2 cities remain electrified.
- The 4-th event breaks Power Line 10 that connects Point 1 and Point 8.
- The 5-th event breaks Power Line 2 that connects Point 4 and Point 10.
- The 6-th event breaks Power Line 7 that connects Point 1 and Point 7.
- Now City 1 is no longer electrified, while 1 city remains electrified.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
Q 個の整数の 3 つ組 (a_1, b_1, d_1), (a_2, b_2, d_2), \ldots, (a_Q, b_Q, d_Q) が与えられます。
集合 \lbrace 1, 2, \ldots, Q\rbrace の部分集合 S が良い集合であることを、 下記の条件を満たす長さ N の整数列 (X_1, X_2, \ldots, X_N) が存在することと定めます。
すべての i \in S について、X_{a_i} - X_{b_i} = d_i が成り立つ。
S が空集合である状態から始め、i = 1, 2, \ldots, Q の順に下記の操作を行います。
もし S \cup \lbrace i \rbrace が良い集合なら、S を S \cup \lbrace i \rbrace で置き換える。
最終的な S のすべての要素を昇順に出力してください。
制約
- 入力される値はすべて整数
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq a_i, b_i \leq N
- -10^9 \leq d_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q a_1 b_1 d_1 a_2 b_2 d_2 \vdots a_Q b_Q d_Q
出力
最終的な S のすべての要素を昇順に並べた列 (s_1, s_2, \ldots, s_k) を、下記の形式で空白区切りで出力せよ。
s_1 s_2 \ldots s_k
入力例 1
3 5 1 2 2 3 2 -3 2 1 -1 3 3 0 1 3 5
出力例 1
1 2 4 5
S が空集合である状態から始め、i = 1, 2, 3, 4, 5 の順に問題文中の操作を下記の通り行います。
- i = 1 について、集合 S \cup \lbrace i \rbrace = \lbrace 1 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, 4) が問題文中の条件を満たすからです。よって、S を \lbrace 1\rbrace で置き換えます。
- i = 2 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2\rbrace で置き換えます。
- i = 3 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 3 \rbrace は良い集合ではありません。
- i = 4 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 4 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2, 4\rbrace で置き換えます。
- i = 5 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 4, 5 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2, 4, 5\rbrace で置き換えます。
よって、最終的な S は \lbrace 1, 2, 4, 5\rbrace です。
入力例 2
200000 1 1 1 1
出力例 2
最終的な S は空集合です。
入力例 3
5 20 4 2 125421359 2 5 -191096267 3 4 -42422908 3 5 -180492387 3 3 174861038 2 3 -82998451 3 4 -134761089 3 1 -57159320 5 2 191096267 2 4 -120557647 4 2 125421359 2 3 142216401 4 5 -96172984 3 5 -108097816 1 5 -50938496 1 2 140157771 5 4 65674908 4 3 35196193 4 4 0 3 4 188711840
出力例 3
1 2 3 6 8 9 11 14 15 16 17 19
Score : 525 points
Problem Statement
You are given Q triples of integers (a_1, b_1, d_1), (a_2, b_2, d_2), \ldots, (a_Q, b_Q, d_Q).
A subset S of the set \lbrace 1, 2, \ldots, Q\rbrace is defined to be a good set if there exists an integer sequence (X_1, X_2, \ldots, X_N) of length N that satisfies:
X_{a_i} - X_{b_i} = d_i for all i \in S.
Starting with S as an empty set, perform the following operation for i = 1, 2, \ldots, Q in this order:
If S \cup \lbrace i \rbrace is a good set, then replace S with S \cup \lbrace i \rbrace.
Print all elements of the final set S in ascending order.
Constraints
- All input values are integers.
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq a_i, b_i \leq N
- -10^9 \leq d_i \leq 10^9
Input
The input is given from Standard Input in the following format:
N Q a_1 b_1 d_1 a_2 b_2 d_2 \vdots a_Q b_Q d_Q
Output
Print the sequence (s_1, s_2, \ldots, s_k) of all elements of the final set S in ascending order, separated by spaces, in the following format:
s_1 s_2 \ldots s_k
Sample Input 1
3 5 1 2 2 3 2 -3 2 1 -1 3 3 0 1 3 5
Sample Output 1
1 2 4 5
Starting with S as an empty set, perform the operation described in the problem statement for i = 1, 2, 3, 4, 5 in this order, as follows.
- For i = 1, the set S \cup \lbrace i \rbrace = \lbrace 1 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, 4) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1\rbrace.
- For i = 2, the set S \cup \lbrace i \rbrace = \lbrace 1, 2 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2\rbrace.
- For i = 3, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 3 \rbrace is not a good set.
- For i = 4, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 4 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2, 4\rbrace.
- For i = 5, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 4, 5 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2, 4, 5\rbrace.
Therefore, the final set S is \lbrace 1, 2, 4, 5\rbrace.
Sample Input 2
200000 1 1 1 1
Sample Output 2
The final set S is empty.
Sample Input 3
5 20 4 2 125421359 2 5 -191096267 3 4 -42422908 3 5 -180492387 3 3 174861038 2 3 -82998451 3 4 -134761089 3 1 -57159320 5 2 191096267 2 4 -120557647 4 2 125421359 2 3 142216401 4 5 -96172984 3 5 -108097816 1 5 -50938496 1 2 140157771 5 4 65674908 4 3 35196193 4 4 0 3 4 188711840
Sample Output 3
1 2 3 6 8 9 11 14 15 16 17 19