実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋くんは、朝食にご飯、味噌汁、サラダを 1 皿ずつ食べます。
高橋くんの家のテーブルは細長いので、 3 皿を横一列に並べました。並べ方は文字列 S によって与えられ、左から i 番目の皿は S_i が R ならご飯、 S_i が M なら味噌汁、 S_i が S ならサラダです。
ご飯の皿が味噌汁の皿より左にあるかどうかを判定してください。
制約
- |S| = 3
- S には
RMSが 1 文字ずつ含まれる
入力
入力は以下の形式で標準入力から与えられる。
S
出力
ご飯の皿が味噌汁の皿より左にあれば Yes 、そうでなければ No を出力してください。
入力例 1
RSM
出力例 1
Yes
ご飯の皿は左から 1 番目にあり、味噌汁の皿は左から 3 番目にあります。ご飯の皿の方が左にあるので、 Yes を出力します。
入力例 2
SMR
出力例 2
No
左から順に、サラダの皿、味噌汁の皿、ご飯の皿と並んでいます。
Score : 100 points
Problem Statement
Takahashi eats three plates for breakfast: rice, miso soup, and salad.
His table is long and narrow, so he arranged the three plates in a row. The arrangement is given by a string S, where the i-th plate from the left is rice if S_i is R, miso soup if S_i is M, and salad if S_i is S.
Determine whether the plate of rice is to the left of the plate of miso soup.
Constraints
- |S| = 3
- S contains one
R, oneM, and oneS.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if the plate of rice is to the left of the plate of miso soup, and No otherwise.
Sample Input 1
RSM
Sample Output 1
Yes
The plate of rice is at the 1st position from the left, and the plate of miso soup is at the 3rd position from the left. Since the plate of rice is to the left, print Yes.
Sample Input 2
SMR
Sample Output 2
No
The plates are arranged as salad, miso soup, and rice from left to right.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
文字列が N 個与えられます。
i 番目 (1\leq i\leq N) に与えられる文字列 S _ i は Takahashi か Aoki のどちらかと等しいです。
S _ i が Takahashi と等しい i がいくつあるか求めてください。
制約
- 1\leq N\leq 100
- N は整数
- S _ i は
TakahashiかAokiのいずれか (1\leq i\leq N)
入力
入力は以下の形式で標準入力から与えられる。
N S _ 1 S _ 2 \vdots S _ N
出力
S _ i が Takahashi と等しい i の個数を整数として一行に出力せよ。
入力例 1
3 Aoki Takahashi Takahashi
出力例 1
2
S _ 2,S _ 3 の 2 つが Takahashi と等しく、S _ 1 はそうではありません。
よって、2 を出力してください。
入力例 2
2 Aoki Aoki
出力例 2
0
Takahashi と等しい S _ i が存在しないこともあります。
入力例 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
出力例 3
7
Score : 100 points
Problem Statement
You are given N strings.
The i-th string S_i (1 \leq i \leq N) is either Takahashi or Aoki.
How many i are there such that S_i is equal to Takahashi?
Constraints
- 1 \leq N \leq 100
- N is an integer.
- Each S_i is
TakahashiorAoki. (1 \leq i \leq N)
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the count of i such that S_i is equal to Takahashi as an integer in a single line.
Sample Input 1
3 Aoki Takahashi Takahashi
Sample Output 1
2
S_2 and S_3 are equal to Takahashi, while S_1 is not.
Therefore, print 2.
Sample Input 2
2 Aoki Aoki
Sample Output 2
0
It is possible that no S_i is equal to Takahashi.
Sample Input 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
長さ N の文字列 S が与えられます。
S に連続して含まれる na を全て nya に置き換えて得られる文字列を答えてください。
制約
- N は 1 以上 1000 以下の整数
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
4 naan
出力例 1
nyaan
naan に連続して含まれる na を全て nya に置き換えて得られる文字列は nyaan です。
入力例 2
4 near
出力例 2
near
S に na が連続して含まれないこともあります。
入力例 3
8 national
出力例 3
nyationyal
Score : 200 points
Problem Statement
You are given a string S of length N.
Find the string obtained by replacing all contiguous occurrences of na in S with nya.
Constraints
- N is an integer between 1 and 1000, inclusive.
- 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
Print the answer.
Sample Input 1
4 naan
Sample Output 1
nyaan
Replacing all contiguous occurrences of na in naan with nya results in the string nyaan.
Sample Input 2
4 near
Sample Output 2
near
S may not contain a contiguous na.
Sample Input 3
8 national
Sample Output 3
nyationyal
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
2 次元座標平面の原点に高橋くんがいます。
高橋くんが座標平面上の点 (a,b) から点 (c,d) に移動するには \sqrt{(a-c)^2+(b-d)^2} のコストがかかります。
高橋くんが原点からスタートし N 個の点 (X_1,Y_1),\ldots,(X_N,Y_N) へこの順に移動したのち原点に戻るときの、コストの総和を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq X_i,Y_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 \vdots X_N Y_N
出力
答えを出力せよ。
真の値との相対誤差または絶対誤差が 10^{-6} 以下であれば正解とみなされる。
入力例 1
2 1 2 -1 0
出力例 1
6.06449510224597979401
移動は次の 3 行程からなります。
- (0,0) から (1,2) に移動する。\sqrt{(0-1)^2+(0-2)^2}=\sqrt{5}=2.236067977... のコストがかかる
- (1,2) から (-1,0) に移動する。\sqrt{(1-(-1))^2+(2-0)^2}=\sqrt{8}=2.828427124... のコストがかかる
- (-1,0) から (0,0) に移動する。\sqrt{(-1-0)^2+(0-0)^2}=\sqrt{1}=1 のコストがかかる
コストの総和は 6.064495102... となります。
入力例 2
7 -14142 13562 -17320 50807 -22360 67977 24494 89742 -26457 51311 28284 27124 31622 77660
出力例 2
384694.57587932075868509383
入力例 3
5 -100000 100000 100000 -100000 -100000 100000 100000 -100000 -100000 100000
出力例 3
1414213.56237309504880168872
Score : 150 points
Problem Statement
Takahashi is at the origin on a two-dimensional coordinate plane.
The cost for him to move from point (a, b) to point (c, d) is \sqrt{(a - c)^2 + (b - d)^2}.
Find the total cost when he starts at the origin, visits N points (X_1, Y_1), \ldots, (X_N, Y_N) in this order, and then returns to the origin.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq X_i, Y_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 \vdots X_N Y_N
Output
Print the answer.
Your output will be considered correct if its absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
2 1 2 -1 0
Sample Output 1
6.06449510224597979401
The journey consists of the following three steps:
- Move from (0, 0) to (1, 2). The cost is \sqrt{(0 - 1)^2 + (0 - 2)^2} = \sqrt{5} = 2.236067977....
- Move from (1, 2) to (-1, 0). The cost is \sqrt{(1 - (-1))^2 + (2 - 0)^2} = \sqrt{8} = 2.828427124....
- Move from (-1, 0) to (0, 0). The cost is \sqrt{(-1 - 0)^2 + (0 - 0)^2} = \sqrt{1} = 1.
The total cost is 6.064495102....
Sample Input 2
7 -14142 13562 -17320 50807 -22360 67977 24494 89742 -26457 51311 28284 27124 31622 77660
Sample Output 2
384694.57587932075868509383
Sample Input 3
5 -100000 100000 100000 -100000 -100000 100000 100000 -100000 -100000 100000
Sample Output 3
1414213.56237309504880168872
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 人の研究者がおり、研究者には 1, 2, \ldots, N の番号が付けられています。
研究者の間には M 個の利害関係があり、i = 1, 2, \ldots, M に対して研究者 A_i と研究者 B_i は互いに利害関係にあります。
論文の査読者は、その論文の著者とは異なり、著者と利害関係にない相異なる 3 人の研究者である必要があります。
i = 1, 2, \ldots, N について以下の問題を解いてください。
- 研究者 i が著者である論文の査読者の 3 人組として考えられるものは何通りあるか求めよ。
ただし、すべての論文は単著であるものとします。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 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)
- i \neq j のとき (A_i, B_i) \neq (B_j, A_j)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
i = 1, 2, \ldots, N に対する答えをこの順に空白区切りで出力せよ。
入力例 1
6 5 1 2 1 4 2 3 5 3 3 1
出力例 1
0 1 0 4 4 10
以下、研究者の番号の集合により研究者の集合を表します。
-
研究者 1 が著者である論文の査読者の 3 人組として考えられるものはありません。
-
研究者 2 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 4, 5, 6 \rbrace の 1 通りです。
-
研究者 3 が著者である論文の査読者の 3 人組として考えられるものはありません。
-
研究者 4 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 2, 3, 5 \rbrace, \lbrace 2, 3, 6 \rbrace, \lbrace 2, 5, 6 \rbrace, \lbrace 3, 5, 6 \rbrace の 4 通りです。
-
研究者 5 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 6 \rbrace, \lbrace 1, 4, 6 \rbrace, \lbrace 2, 4, 6 \rbrace の 4 通りです。
-
研究者 6 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 1, 2, 3 \rbrace, \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 5 \rbrace, \lbrace 1, 3, 4 \rbrace, \lbrace 1, 3, 5 \rbrace, \lbrace 1, 4, 5 \rbrace, \lbrace 2, 3, 4 \rbrace, \lbrace 2, 3, 5 \rbrace, \lbrace 2, 4, 5 \rbrace, \lbrace 3, 4, 5 \rbrace の 10 通りです。
入力例 2
7 3 1 2 3 4 5 6
出力例 2
10 10 10 10 10 10 20
入力例 3
6 9 3 6 2 5 2 3 4 3 1 5 6 2 3 1 5 3 2 4
出力例 3
1 0 0 1 0 1
Score : 300 points
Problem Statement
There are N researchers, numbered 1, 2, \ldots, N.
There are M conflicts of interest among the researchers; for i = 1, 2, \ldots, M, researchers A_i and B_i have a conflict of interest with each other.
The reviewers of a paper must be three distinct researchers who are different from the author of the paper and have no conflict of interest with the author.
For i = 1, 2, \ldots, N, solve the following problem:
- Find the number of possible triples of reviewers for a paper authored by researcher i.
Assume that all papers are single-authored.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- (A_i, B_i) \neq (A_j, B_j) if i \neq j.
- (A_i, B_i) \neq (B_j, A_j) if i \neq j.
- All input values are integers.
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
Print the answers for i = 1, 2, \ldots, N in this order, separated by spaces.
Sample Input 1
6 5 1 2 1 4 2 3 5 3 3 1
Sample Output 1
0 1 0 4 4 10
Below, we represent a set of researchers by the set of their numbers.
-
There are no possible triples of reviewers for a paper authored by researcher 1.
-
The possible triple of reviewers for a paper authored by researcher 2 is \lbrace 4, 5, 6 \rbrace, which is 1 triple.
-
There are no possible triples of reviewers for a paper authored by researcher 3.
-
The possible triples of reviewers for a paper authored by researcher 4 are \lbrace 2, 3, 5 \rbrace, \lbrace 2, 3, 6 \rbrace, \lbrace 2, 5, 6 \rbrace, \lbrace 3, 5, 6 \rbrace, which is 4 triples.
-
The possible triples of reviewers for a paper authored by researcher 5 are \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 6 \rbrace, \lbrace 1, 4, 6 \rbrace, \lbrace 2, 4, 6 \rbrace, which is 4 triples.
-
The possible triples of reviewers for a paper authored by researcher 6 are \lbrace 1, 2, 3 \rbrace, \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 5 \rbrace, \lbrace 1, 3, 4 \rbrace, \lbrace 1, 3, 5 \rbrace, \lbrace 1, 4, 5 \rbrace, \lbrace 2, 3, 4 \rbrace, \lbrace 2, 3, 5 \rbrace, \lbrace 2, 4, 5 \rbrace, \lbrace 3, 4, 5 \rbrace, which is 10 triples.
Sample Input 2
7 3 1 2 3 4 5 6
Sample Output 2
10 10 10 10 10 10 20
Sample Input 3
6 9 3 6 2 5 2 3 4 3 1 5 6 2 3 1 5 3 2 4
Sample Output 3
1 0 0 1 0 1
実行時間制限: 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 点
問題文
スーパーマンである高橋くんは、地上で困っている人を助けるため、あるビルの屋上から飛び降りようとしています。 高橋くんがいる星には重力の大きさを表す g という値が定まっており、 高橋くんが落下を開始してから地面に到達するまでにかかる時間は \frac{A}{\sqrt{g}} です。
現在の時刻は 0 であり、g = 1 が成り立ちます。 高橋くんは、今から次の操作を好きな回数(0 回でもよい)行います。
- 超能力により g の値を 1 増やす。時間が B 経過する。
その後、高橋くんはビルから飛び降ります。落下を開始した後は g の値を変えることはできません。 また、操作によって経過する時間と落下にかかる時間以外は考えないものとします。
高橋くんが地面に到達できる最も早い時刻を求めてください。
制約
- 1 \leq A \leq 10^{18}
- 1 \leq B \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
高橋くんが地面に到達できる最も早い時刻を出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
10 1
出力例 1
7.7735026919
- 操作を 0 回行うとき、地面に到達する時刻は 1\times 0+\frac{10}{\sqrt{1}} = 10 です。
- 操作を 1 回行うとき、地面に到達する時刻は 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07 です。
- 操作を 2 回行うとき、地面に到達する時刻は 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77 です。
- 操作を 3 回行うとき、地面に到達する時刻は 1\times 3+\frac{10}{\sqrt{4}} = 8 です。
操作を 4 回以上行っても、地面への到達時刻は遅くなるのみです。 よって、操作を 2 回行ってから飛び降りるのが最適で、答えは 2+\frac{10}{\sqrt{3}} です。
入力例 2
5 10
出力例 2
5.0000000000
操作を 1 回も行わないのが最適です。
入力例 3
1000000000000000000 100
出力例 3
8772053214538.5976562500
Score : 400 points
Problem Statement
A superman, Takahashi, is about to jump off the roof of a building to help a person in trouble on the ground. Takahashi's planet has a constant value g that represents the strength of gravity, and the time it takes for him to reach the ground after starting to fall is \frac{A}{\sqrt{g}}.
It is now time 0, and g = 1. Takahashi will perform the following operation as many times as he wants (possibly zero).
- Use a superpower to increase the value of g by 1. This takes a time of B.
Then, he will jump off the building. After starting to fall, he cannot change the value of g. Additionally, we only consider the time it takes to perform the operation and fall.
Find the earliest time Takahashi can reach the ground.
Constraints
- 1 \leq A \leq 10^{18}
- 1 \leq B \leq 10^{18}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print the earliest time Takahashi can reach the ground. Your output will be accepted when its absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
10 1
Sample Output 1
7.7735026919
- If he performs the operation zero times, he will reach the ground at time 1\times 0+\frac{10}{\sqrt{1}} = 10.
- If he performs the operation once, he will reach the ground at time 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07.
- If he performs the operation twice, he will reach the ground at time 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77.
- If he performs the operation three times, he will reach the ground at time 1\times 3+\frac{10}{\sqrt{4}} = 8.
Performing the operation four or more times will only delay the time to reach the ground. Therefore, it is optimal to perform the operation twice before jumping off, and the answer is 2+\frac{10}{\sqrt{3}}.
Sample Input 2
5 10
Sample Output 2
5.0000000000
It is optimal not to perform the operation at all.
Sample Input 3
1000000000000000000 100
Sample Output 3
8772053214538.5976562500
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純連結無向グラフが与えられます。
辺 i は頂点 A_i と頂点 B_i を結ぶ長さ C_i の辺です。
以下の条件を満たすようにいくつかの辺を削除します。削除する辺の数の最大値を求めてください。
- 辺を削除した後のグラフも連結である。
- 全ての頂点対 (s,t) について、頂点 s と頂点 t の間の距離が削除前と削除後で変化しない。
注釈
単純連結無向グラフとは、単純かつ連結で辺に向きの無いグラフのことをいいます。
グラフが単純であるとは、グラフが自己ループや多重辺を含まないことをいいます。
グラフが連結であるとは、グラフ上の任意の 2 頂点 s, t について s から t へ辺をたどって行けることをいいます。
頂点 s と頂点 t の間の距離とは、頂点 s と頂点 t の間の最短路の長さのことをいいます。
制約
- 2 \leq N \leq 300
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i \lt B_i \leq N
- 1 \leq C_i \leq 10^9
- i \neq j ならば (A_i, B_i) \neq (A_j, B_j) である。
- 与えられるグラフは連結である。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
答えを出力せよ。
入力例 1
3 3 1 2 2 2 3 3 1 3 6
出力例 1
1
辺を削除する前の全ての頂点対の距離は次の通りです。
- 頂点 1 と頂点 2 の距離は 2
- 頂点 1 と頂点 3 の距離は 5
- 頂点 2 と頂点 3 の距離は 3
辺 3 を削除しても全ての頂点間の距離は変化しません。また、問題文の条件を満たすように 2 本以上の辺を削除することはできないので、答えは 1 本になります。
入力例 2
5 4 1 3 3 2 3 9 3 5 3 4 5 3
出力例 2
0
どの辺も削除することができません。
入力例 3
5 10 1 2 71 1 3 9 1 4 82 1 5 64 2 3 22 2 4 99 2 5 1 3 4 24 3 5 18 4 5 10
出力例 3
5
Score : 500 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges.
Edge i connects Vertex A_i and Vertex B_i, and has a length of C_i.
Let us delete some number of edges while satisfying the conditions below. Find the maximum number of edges that can be deleted.
- The graph is still connected after the deletion.
- For every pair of vertices (s, t), the distance between s and t remains the same before and after the deletion.
Notes
A simple connected undirected graph is a graph that is simple and connected and has undirected edges.
A graph is simple when it has no self-loops and no multi-edges.
A graph is connected when, for every pair of two vertices s and t, t is reachable from s by traversing edges.
The distance between Vertex s and Vertex t is the length of a shortest path between s and t.
Constraints
- 2 \leq N \leq 300
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i \lt B_i \leq N
- 1 \leq C_i \leq 10^9
- (A_i, B_i) \neq (A_j, B_j) if i \neq j.
- The given graph is connected.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the answer.
Sample Input 1
3 3 1 2 2 2 3 3 1 3 6
Sample Output 1
1
The distance between each pair of vertices before the deletion is as follows.
- The distance between Vertex 1 and Vertex 2 is 2.
- The distance between Vertex 1 and Vertex 3 is 5.
- The distance between Vertex 2 and Vertex 3 is 3.
Deleting Edge 3 does not affect the distance between any pair of vertices. It is impossible to delete two or more edges while satisfying the condition, so the answer is 1.
Sample Input 2
5 4 1 3 3 2 3 9 3 5 3 4 5 3
Sample Output 2
0
No edge can be deleted.
Sample Input 3
5 10 1 2 71 1 3 9 1 4 82 1 5 64 2 3 22 2 4 99 2 5 1 3 4 24 3 5 18 4 5 10
Sample Output 3
5
実行時間制限: 6 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 次元空間上の 2 点 x=(x_1, x_2, \dots, x_N), y = (y_1, y_2, \dots, y_N) のマンハッタン距離 d(x,y) は次の式で定義されます。
また、座標成分 x_1, x_2, \dots, x_N がすべて整数であるような点 x=(x_1, x_2, \dots, x_N) を格子点と呼びます。
N 次元空間上の格子点 p=(p_1, p_2, \dots, p_N), q = (q_1, q_2, \dots, q_N) が与えられます。
d(p,r) \leq D かつ d(q,r) \leq D であるような格子点 r としてあり得るものは全部で何個ありますか?答えを 998244353 で割ったあまりを求めてください。
制約
- 1 \leq N \leq 100
- 0 \leq D \leq 1000
- -1000 \leq p_i, q_i \leq 1000
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D p_1 p_2 \dots p_N q_1 q_2 \dots q_N
出力
答えを出力せよ。
入力例 1
1 5 0 3
出力例 1
8
N=1 の場合は 1 次元空間、すなわち数直線上の点に関する問題になります。
条件を満たす点は -2,-1,0,1,2,3,4,5 の 8 個です。
入力例 2
3 10 2 6 5 2 1 2
出力例 2
632
入力例 3
10 100 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
出力例 3
145428186
Score : 500 points
Problem Statement
In an N-dimensional space, the Manhattan distance d(x,y) between two points x=(x_1, x_2, \dots, x_N) and y = (y_1, y_2, \dots, y_N) is defined by:
A point x=(x_1, x_2, \dots, x_N) is said to be a lattice point if the components x_1, x_2, \dots, x_N are all integers.
You are given lattice points p=(p_1, p_2, \dots, p_N) and q = (q_1, q_2, \dots, q_N) in an N-dimensional space.
How many lattice points r satisfy d(p,r) \leq D and d(q,r) \leq D? Find the count modulo 998244353.
Constraints
- 1 \leq N \leq 100
- 0 \leq D \leq 1000
- -1000 \leq p_i, q_i \leq 1000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N D p_1 p_2 \dots p_N q_1 q_2 \dots q_N
Output
Print the answer.
Sample Input 1
1 5 0 3
Sample Output 1
8
When N=1, we consider points in a one-dimensional space, that is, on a number line.
8 lattice points satisfy the conditions: -2,-1,0,1,2,3,4,5.
Sample Input 2
3 10 2 6 5 2 1 2
Sample Output 2
632
Sample Input 3
10 100 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
Sample Output 3
145428186