Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は手芸クラブの部長です。文化祭の準備として、N 本のリボンをそれぞれ決められた長さに切り分けて、飾り付け用のパーツを作ることになりました。
i 番目のリボンの長さは L_i ミリメートルで、パーツ1つあたりの指定された長さは T_i ミリメートルです。各リボンについて、一方の端から順に T_i ミリメートルずつ切り出していきます。こうして得られるパーツの個数は \lfloor L_i / T_i \rfloor 個であり、切り出した後に残る切れ端の長さは R_i = L_i \bmod T_i ミリメートルです。
顧問の青木先生は「切れ端が残っていて(R_i > 0)、かつその長さが指定された長さの半分以上、すなわち R_i \geq \lfloor T_i / 2 \rfloor を満たすならば、その切れ端も1つのパーツとして使ってよい」というルールを設けています。
まとめると、i 番目のリボンから得られるパーツの個数 P_i は次のように決まります。
P_i = \begin{cases} \lfloor L_i / T_i \rfloor + 1 & \text{(} R_i > 0 \text{ かつ } R_i \geq \lfloor T_i / 2 \rfloor \text{ の場合)} \\ \lfloor L_i / T_i \rfloor & \text{(それ以外の場合)} \end{cases}
ここで「それ以外の場合」とは、切れ端が残らない場合(R_i = 0)、または切れ端が残るが長さが \lfloor T_i / 2 \rfloor に満たない場合(0 < R_i < \lfloor T_i / 2 \rfloor)のいずれかです。
なお、L_i < T_i の場合にもこのルールは同様に適用されます。この場合 \lfloor L_i / T_i \rfloor = 0 であり、リボン全体が長さ R_i = L_i の切れ端となります。切れ端の長さが \lfloor T_i / 2 \rfloor 以上であればパーツ1個が得られ、そうでなければパーツは0個です。
N 本のリボンについて、それぞれ得られるパーツの個数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_i \leq 10^{18}
- 1 \leq T_i \leq 10^{18}
- 入力はすべて整数である。
入力
N L_1 T_1 L_2 T_2 \vdots L_N T_N
1 行目には、リボンの本数を表す整数 N が与えられる。続く N 行のうち i 行目(1 \leq i \leq N)には、i 番目のリボンの長さ L_i と指定されたパーツの長さ T_i が、スペース区切りで与えられる。
出力
N 行出力せよ。i 行目(1 \leq i \leq N)には、i 番目のリボンから得られるパーツの個数を出力せよ。
入力例 1
5 10 3 10 5 7 4 7 5 7 6
出力例 1
4 2 2 2 1
入力例 2
5 5 3 4 3 3 7 2 7 100 100
出力例 2
2 2 1 0 1
入力例 3
8 1000 300 1000 400 999 500 123456 789 50 99 50 100 50 101 1 1
出力例 3
3 3 2 156 1 1 1 1
入力例 4
10 1000000000000000000 1 1000000000000000000 3 999999999999999999 2 1000000000000000000 1000000000000000000 999999999999999999 1000000000000000000 500000000000000000 1000000000000000000 499999999999999999 1000000000000000000 7 2 123456789012345678 987654321 1000000000000000000 7
出力例 4
1000000000000000000 333333333333333334 500000000000000000 1 1 1 0 4 124999999 142857142857142857
入力例 5
6 1 1 1 2 1 3 1 1000000000000000000 1000000000000000000 1000000000000000000 2 1
出力例 5
1 1 1 0 1 2
Score : 233 pts
Problem Statement
Takahashi is the president of the handicrafts club. As preparation for the cultural festival, he needs to cut N ribbons into specified lengths to make decorative parts.
The i-th ribbon has a length of L_i millimeters, and the specified length per part is T_i millimeters. For each ribbon, parts of T_i millimeters are cut out sequentially from one end. The number of parts obtained this way is \lfloor L_i / T_i \rfloor, and the length of the leftover scrap after cutting is R_i = L_i \bmod T_i millimeters.
The advisor, Teacher Aoki, has established the following rule: "If there is a remaining scrap (R_i > 0) and its length is at least half of the specified length, that is, R_i \geq \lfloor T_i / 2 \rfloor, then that scrap may also be used as one part."
In summary, the number of parts P_i obtained from the i-th ribbon is determined as follows:
P_i = \begin{cases} \lfloor L_i / T_i \rfloor + 1 & \text{(if } R_i > 0 \text{ and } R_i \geq \lfloor T_i / 2 \rfloor \text{)} \\ \lfloor L_i / T_i \rfloor & \text{(otherwise)} \end{cases}
Here, "otherwise" refers to either the case where no scrap remains (R_i = 0), or the case where a scrap remains but its length is less than \lfloor T_i / 2 \rfloor (0 < R_i < \lfloor T_i / 2 \rfloor).
Note that this rule applies in the same way even when L_i < T_i. In this case, \lfloor L_i / T_i \rfloor = 0, and the entire ribbon becomes a scrap of length R_i = L_i. If the scrap length is at least \lfloor T_i / 2 \rfloor, then one part is obtained; otherwise, zero parts are obtained.
For each of the N ribbons, determine the number of parts obtained.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_i \leq 10^{18}
- 1 \leq T_i \leq 10^{18}
- All input values are integers.
Input
N L_1 T_1 L_2 T_2 \vdots L_N T_N
The first line contains an integer N representing the number of ribbons. Each of the following N lines, where the i-th line (1 \leq i \leq N), contains the length L_i of the i-th ribbon and the specified part length T_i, separated by a space.
Output
Output N lines. The i-th line (1 \leq i \leq N) should contain the number of parts obtained from the i-th ribbon.
Sample Input 1
5 10 3 10 5 7 4 7 5 7 6
Sample Output 1
4 2 2 2 1
Sample Input 2
5 5 3 4 3 3 7 2 7 100 100
Sample Output 2
2 2 1 0 1
Sample Input 3
8 1000 300 1000 400 999 500 123456 789 50 99 50 100 50 101 1 1
Sample Output 3
3 3 2 156 1 1 1 1
Sample Input 4
10 1000000000000000000 1 1000000000000000000 3 999999999999999999 2 1000000000000000000 1000000000000000000 999999999999999999 1000000000000000000 500000000000000000 1000000000000000000 499999999999999999 1000000000000000000 7 2 123456789012345678 987654321 1000000000000000000 7
Sample Output 4
1000000000000000000 333333333333333334 500000000000000000 1 1 1 0 4 124999999 142857142857142857
Sample Input 5
6 1 1 1 2 1 3 1 1000000000000000000 1000000000000000000 1000000000000000000 2 1
Sample Output 5
1 1 1 0 1 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は、ある数列の分析を行っています。
N 個の整数からなる数列 A_1, A_2, \ldots, A_N が与えられます。高橋君は、この数列から 連続する区間 を選び、その要素の合計が最大となるようにしたいと考えています。
具体的には、1 \leq l \leq r \leq N を満たす整数の組 (l, r) を選んで、区間の合計 A_l + A_{l+1} + \cdots + A_r を求めます。
また、要素を 1 つも選ばないこと(空の区間を選ぶこと)も許されており、その場合の合計は 0 とします。
これらすべての選び方における合計の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq A_i \leq 10^9
- 入力はすべて整数
入力
N A_1 A_2 \cdots A_N
- 1 行目には、数列の要素数を表す整数 N が与えられる。
- 2 行目には、数列の各要素を表す整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
連続する区間の合計(空の区間の場合は 0)の最大値を 1 行で出力せよ。
入力例 1
5 3 -1 4 -2 2
出力例 1
6
入力例 2
8 -2 1 -3 4 -1 2 1 -5
出力例 2
6
入力例 3
10 -5 -3 -1 -2 -4 -6 -1 -8 -2 -7
出力例 3
0
Score : 333 pts
Problem Statement
Takahashi is analyzing a certain sequence.
He is given a sequence of N integers A_1, A_2, \ldots, A_N. Takahashi wants to select a contiguous subarray from this sequence such that the sum of its elements is maximized.
Specifically, he chooses a pair of integers (l, r) satisfying 1 \leq l \leq r \leq N, and computes the subarray sum A_l + A_{l+1} + \cdots + A_r.
It is also permitted to select no elements (i.e., to choose an empty subarray), in which case the sum is defined to be 0.
Find the maximum sum among all possible choices.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq A_i \leq 10^9
- All input values are integers.
Input
N A_1 A_2 \cdots A_N
- The first line contains an integer N, representing the number of elements in the sequence.
- The second line contains the integers A_1, A_2, \ldots, A_N, representing the elements of the sequence, separated by spaces.
Output
Print the maximum sum of a contiguous subarray (or 0 in the case of an empty subarray) on a single line.
Sample Input 1
5 3 -1 4 -2 2
Sample Output 1
6
Sample Input 2
8 -2 1 -3 4 -1 2 1 -5
Sample Output 2
6
Sample Input 3
10 -5 -3 -1 -2 -4 -6 -1 -8 -2 -7
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君の部屋には、1 から N までの番号がついた N 個の照明があります。最初、すべての照明は消灯しています。
高橋君はこれから M 回の操作を行います。j 回目 (1 \leq j \leq M) の操作では、番号が L_j 以上 R_j 以下であるすべての照明のスイッチを切り替えます。すなわち、それらの照明のうち点灯しているものは消灯に、消灯しているものは点灯に変わります。
M 回すべての操作を終えた後、点灯している照明の個数を求めてください。
制約
- 1 \leq N \leq 10^6
- 1 \leq M \leq 2 \times 10^5
- 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
- 入力はすべて整数である
入力
N M L_1 R_1 L_2 R_2 \vdots L_M R_M
1 行目には、照明の個数 N と操作の回数 M がスペース区切りで与えられます。続く M 行のうち j 行目 (1 \leq j \leq M) には、j 回目の操作で切り替える区間の左端 L_j と右端 R_j がスペース区切りで与えられます。
出力
M 回の操作をすべて終えた後に点灯している照明の個数を、1 行で出力してください。
入力例 1
5 2 1 3 2 4
出力例 1
2
入力例 2
6 3 1 4 2 5 3 6
出力例 2
4
入力例 3
20 5 1 10 5 15 3 8 1 20 10 15
出力例 3
13
入力例 4
1000 6 1 500 250 750 1 1000 400 600 1 100 800 900
出力例 4
499
入力例 5
1 1 1 1
出力例 5
1
Score : 366 pts
Problem Statement
Takahashi's room has N lights numbered from 1 to N. Initially, all lights are turned off.
Takahashi will perform M operations. In the j-th operation (1 \leq j \leq M), he toggles the switches of all lights whose numbers are between L_j and R_j, inclusive. That is, among those lights, the ones that are on are turned off, and the ones that are off are turned on.
Find the number of lights that are on after all M operations have been completed.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq M \leq 2 \times 10^5
- 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
- All input values are integers
Input
N M L_1 R_1 L_2 R_2 \vdots L_M R_M
The first line contains the number of lights N and the number of operations M, separated by a space. The j-th line (1 \leq j \leq M) of the following M lines contains the left endpoint L_j and the right endpoint R_j of the interval to be toggled in the j-th operation, separated by a space.
Output
Print the number of lights that are on after all M operations have been completed, on a single line.
Sample Input 1
5 2 1 3 2 4
Sample Output 1
2
Sample Input 2
6 3 1 4 2 5 3 6
Sample Output 2
4
Sample Input 3
20 5 1 10 5 15 3 8 1 20 10 15
Sample Output 3
13
Sample Input 4
1000 6 1 500 250 750 1 1000 400 600 1 100 800 900
Sample Output 4
499
Sample Input 5
1 1 1 1
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は毎日、自宅から学校まで歩いて通学しています。最近、通学路の途中にある地点の近くで野犬が目撃されるようになりました。野犬は特定の地点を拠点にしており、その周辺をうろついています。
高橋君は、通学路として使う経路を工夫することで、野犬の拠点からできるだけ離れた経路を通りたいと考えています。
街は N 個の地点と M 本の双方向の道で構成されています。各地点には 1 から N までの番号が付いています。各道 i (1 \leq i \leq M) は地点 U_i と地点 V_i を結び、通過するのに W_i 分かかります(W_i は正の整数)。
高橋君の自宅は地点 S、学校は地点 T、野犬の拠点は地点 P です。
野犬の拠点である地点 P からある地点 X への最短所要時間 d(X) を次のように定義します。
- d(P) = 0 とします。
- 地点 P から地点 X(X \neq P)に到達可能であるとき、d(X) は地点 P から地点 X へ至る経路における所要時間(通過する道の W_i の合計)の最小値です。
- 地点 P から地点 X に到達できない場合は d(X) = +\infty とします。
ここで、+\infty は任意の有限の値より大きいものとします。
地点 S から地点 T への単純パスとは、地点の列 v_0, v_1, \ldots, v_k(ただし k \geq 1, v_0 = S, v_k = T)であって、以下の条件をすべて満たすものです。
- v_0, v_1, \ldots, v_k はすべて相異なる(同じ地点を 2 度以上通らない)。
- すべての 1 \leq j \leq k に対し、地点 v_{j-1} と地点 v_j を結ぶ道が存在する。
ある単純パス v_0, v_1, \ldots, v_k の安全度を、始点・終点を含む経路上のすべての地点における d の最小値、すなわち
\min(d(v_0), d(v_1), \ldots, d(v_k))
として定義します。ここで、d の値に有限の値と +\infty が混在する場合、\min は有限の値の中の最小値をとります。すべての d(v_j) が +\infty である場合に限り、安全度は +\infty となります。
安全度は、経路上で最も野犬の拠点に近い地点がどれだけ拠点から離れているかを表す指標です。
高橋君が選べる地点 S から地点 T への単純パスのうち、安全度が最大となるものの安全度を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq S, T, P \leq N
- S \neq T
- S と P、T と P はそれぞれ一致してもよい
- 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
- U_i \neq V_i (1 \leq i \leq M)
- 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
- 同じ 2 地点を結ぶ道は高々 1 本である
- 地点 S から地点 T へ至る単純パスが少なくとも 1 つ存在する
- 入力はすべて整数である
入力
N M S T P U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- 1 行目には、地点の数 N と道の数 M が、スペース区切りで与えられる。
- 2 行目には、高橋君の自宅の地点番号 S、学校の地点番号 T、野犬の拠点の地点番号 P が、スペース区切りで与えられる。
- 3 行目から M + 2 行目までの M 行で、各道の情報が与えられる。
- 2 + i 行目 (1 \leq i \leq M) には、道 i が結ぶ 2 つの地点 U_i, V_i と、通過にかかる時間 W_i が、スペース区切りで与えられる。
出力
高橋君が選べる単純パスにおける安全度の最大値を 1 行で出力せよ。
安全度の最大値が +\infty となる場合、すなわち経路上のすべての地点が地点 P から到達不可能であるような単純パスが存在する場合は、代わりに INF と出力せよ。
それ以外の場合、安全度の最大値は非負整数となる。その値を出力せよ。
入力例 1
5 5 1 5 3 1 2 1 2 3 2 3 5 1 1 4 3 4 5 3
出力例 1
1
入力例 2
4 3 1 2 4 1 2 1 1 3 2 2 3 3
出力例 2
INF
入力例 3
8 10 1 8 4 1 2 3 2 3 2 3 4 1 4 8 5 1 5 2 5 6 4 6 7 3 7 8 2 2 6 1 3 7 6
出力例 3
4
入力例 4
12 16 1 12 6 1 2 3 2 3 4 3 6 2 6 12 1 1 4 5 4 5 3 5 9 2 9 10 4 10 11 3 11 12 2 4 8 6 8 12 7 2 7 1 7 11 5 5 6 8 8 9 1
出力例 4
1
入力例 5
2 1 1 2 1 1 2 1000000000
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi walks from his home to school every day. Recently, a stray dog has been spotted near a certain point along his school route. The stray dog is based at a specific point and roams around its vicinity.
Takahashi wants to choose a route that stays as far away from the stray dog's base as possible.
The town consists of N points and M bidirectional roads. Each point is numbered from 1 to N. Each road i (1 \leq i \leq M) connects point U_i and point V_i, and takes W_i minutes to traverse (W_i is a positive integer).
Takahashi's home is at point S, his school is at point T, and the stray dog's base is at point P.
The shortest travel time d(X) from the stray dog's base at point P to a point X is defined as follows:
- d(P) = 0.
- When point X (X \neq P) is reachable from point P, d(X) is the minimum travel time (sum of W_i of the roads traversed) over all paths from point P to point X.
- If point X is not reachable from point P, then d(X) = +\infty.
Here, +\infty is considered larger than any finite value.
A simple path from point S to point T is a sequence of points v_0, v_1, \ldots, v_k (where k \geq 1, v_0 = S, v_k = T) satisfying all of the following conditions:
- v_0, v_1, \ldots, v_k are all distinct (no point is visited more than once).
- For all 1 \leq j \leq k, there exists a road connecting point v_{j-1} and point v_j.
The safety score of a simple path v_0, v_1, \ldots, v_k is defined as the minimum value of d over all points on the path, including the start and end points, namely:
\min(d(v_0), d(v_1), \ldots, d(v_k))
Here, if the values of d include both finite values and +\infty, the \min takes the minimum among the finite values. The safety score is +\infty only when all d(v_j) are +\infty.
The safety score represents how far the closest point to the stray dog's base along the route is from that base.
Among all simple paths from point S to point T that Takahashi can choose, find the maximum safety score.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq S, T, P \leq N
- S \neq T
- S and P, as well as T and P, may coincide
- 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
- U_i \neq V_i (1 \leq i \leq M)
- 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
- There is at most one road connecting any pair of points
- There exists at least one simple path from point S to point T
- All input values are integers
Input
N M S T P U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- The first line contains the number of points N and the number of roads M, separated by a space.
- The second line contains the point number S of Takahashi's home, the point number T of his school, and the point number P of the stray dog's base, separated by spaces.
- The following M lines (from line 3 to line M + 2) give the information of each road.
- The (2 + i)-th line (1 \leq i \leq M) contains the two points U_i, V_i connected by road i and the traversal time W_i, separated by spaces.
Output
Output the maximum safety score among all simple paths that Takahashi can choose, in a single line.
If the maximum safety score is +\infty, that is, if there exists a simple path where all points on the path are unreachable from point P, output INF instead.
Otherwise, the maximum safety score is a non-negative integer. Output that value.
Sample Input 1
5 5 1 5 3 1 2 1 2 3 2 3 5 1 1 4 3 4 5 3
Sample Output 1
1
Sample Input 2
4 3 1 2 4 1 2 1 1 3 2 2 3 3
Sample Output 2
INF
Sample Input 3
8 10 1 8 4 1 2 3 2 3 2 3 4 1 4 8 5 1 5 2 5 6 4 6 7 3 7 8 2 2 6 1 3 7 6
Sample Output 3
4
Sample Input 4
12 16 1 12 6 1 2 3 2 3 4 3 6 2 6 12 1 1 4 5 4 5 3 5 9 2 9 10 4 10 11 3 11 12 2 4 8 6 8 12 7 2 7 1 7 11 5 5 6 8 8 9 1
Sample Output 4
1
Sample Input 5
2 1 1 2 1 1 2 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
ある地域には N 個の都市があり、それぞれ 1 から N の番号が付けられています。都市の間には M 本の通信ケーブルが敷設されており、各ケーブルは双方向に通信可能です。 i 番目のケーブル (1 \leq i \leq M) は都市 U_i と都市 V_i を結んでおり、その維持コストは正整数 W_i です。グラフに自己ループや多重辺は存在しません。
このグラフは 2-辺連結です。すなわち、M 本のケーブルのうちどの 1 本を取り除いても、残りのケーブルで任意の 2 つの都市間の通信が可能です。
高橋君はこの地域の通信管理者で、ケーブルの中からちょうど N - 1 本を選び、すべての都市を連結にする「基幹ネットワーク」を構築しようとしています。このような N - 1 本のケーブルの選び方は全域木に対応します。高橋君は、基幹ネットワークに含まれるケーブルの維持コストの合計ができるだけ小さくなるようにケーブルを選びます。すなわち、高橋君は最小全域木を構築します。
一方、青木君はライバル企業のエンジニアです。青木君は高橋君が基幹ネットワークを構築する前に、M 本のケーブルの中からちょうど 1 本を選んで切断することができます。切断されたケーブルは使用不能となり、基幹ネットワークに含めることができなくなります。グラフが 2-辺連結であるため、どのケーブルが切断されても、残りの M - 1 本のケーブルで全域木を構築できることが保証されます。
このゲームの流れは以下の通りです。
- 青木君が M 本のケーブルの中からちょうど 1 本を選んで切断する。
- 高橋君が残りの M - 1 本のケーブルの中から N - 1 本を選び、維持コストの合計が最小となる全域木(最小全域木)を構築する。
青木君は、高橋君が最小全域木を構築することを見越した上で、その維持コストの合計が最大となるように切断するケーブルを選びます。
なお、最小全域木が複数存在する場合でも、その維持コストの合計は一意に定まります。したがって、青木君がどのケーブルを切断するかが決まれば、高橋君が構築する基幹ネットワークの維持コストの合計も一意に定まります。
青木君が最適に妨害し、高橋君が最適にネットワークを構築したとき、基幹ネットワークの維持コストの合計を求めてください。
制約
- 3 \leq N \leq 10^5
- N \leq M \leq 2 \times 10^5
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i(自己ループは存在しない)
- i \neq j ならば (U_i, V_i) \neq (U_j, V_j) かつ (U_i, V_i) \neq (V_j, U_j)(多重辺は存在しない)
- 1 \leq W_i \leq 10^9
- W_i は整数である
- グラフは 2-辺連結である(任意の 1 本の辺を取り除いてもグラフは連結である)
- 答えは 2 \times 10^{14} 以下であることが保証される
入力
N M U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- 1 行目には、都市の数を表す整数 N と、ケーブルの数を表す整数 M が、スペース区切りで与えられる。
- 2 行目から M + 1 行目では、各ケーブルの情報が M 行で与えられる。
- 1 + i 行目 (1 \leq i \leq M) では、 i 番目のケーブルが結ぶ都市の番号 U_i , V_i と、その維持コスト W_i がスペース区切りで与えられる。
出力
青木君が最適に 1 本のケーブルを切断し、高橋君が残りのケーブルから維持コストの合計が最小となるように基幹ネットワークを構築したとき、その維持コストの合計を整数として 1 行で出力せよ。
入力例 1
3 3 1 2 1 2 3 2 1 3 3
出力例 1
5
入力例 2
4 5 1 2 1 2 3 2 3 4 3 4 1 4 1 3 5
出力例 2
9
入力例 3
6 9 1 2 3 2 3 5 3 4 2 4 5 7 5 6 1 6 1 4 1 3 8 2 5 6 4 6 9
出力例 3
20
入力例 4
8 12 1 2 10 2 3 20 3 4 30 4 5 40 5 6 50 6 7 60 7 8 70 8 1 80 1 5 15 2 6 25 3 7 35 4 8 45
出力例 4
210
入力例 5
3 3 1 2 1000000000 2 3 1000000000 1 3 1000000000
出力例 5
2000000000
Score : 466 pts
Problem Statement
There are N cities in a region, numbered from 1 to N. Between the cities, M communication cables have been laid, and each cable allows bidirectional communication. The i-th cable (1 \leq i \leq M) connects city U_i and city V_i, and its maintenance cost is a positive integer W_i. The graph contains no self-loops or multiple edges.
This graph is 2-edge-connected. That is, even if any single cable among the M cables is removed, communication between any two cities remains possible using the remaining cables.
Takahashi is the communication administrator of this region and is trying to build a "backbone network" by selecting exactly N - 1 cables that connect all cities. Such a selection of N - 1 cables corresponds to a spanning tree. Takahashi selects cables so that the total maintenance cost of the cables included in the backbone network is as small as possible. In other words, Takahashi constructs a minimum spanning tree.
On the other hand, Aoki is an engineer from a rival company. Before Takahashi constructs the backbone network, Aoki can choose exactly 1 cable from the M cables and cut it. A cut cable becomes unusable and cannot be included in the backbone network. Since the graph is 2-edge-connected, it is guaranteed that no matter which cable is cut, a spanning tree can still be constructed from the remaining M - 1 cables.
The flow of this game is as follows:
- Aoki chooses exactly 1 cable from the M cables and cuts it.
- Takahashi selects N - 1 cables from the remaining M - 1 cables and constructs a spanning tree with the minimum total maintenance cost (minimum spanning tree).
Aoki chooses which cable to cut so as to maximize the total maintenance cost, anticipating that Takahashi will construct a minimum spanning tree.
Note that even if multiple minimum spanning trees exist, their total maintenance cost is uniquely determined. Therefore, once Aoki decides which cable to cut, the total maintenance cost of the backbone network Takahashi constructs is also uniquely determined.
Find the total maintenance cost of the backbone network when Aoki sabotages optimally and Takahashi constructs the network optimally.
Constraints
- 3 \leq N \leq 10^5
- N \leq M \leq 2 \times 10^5
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i (no self-loops)
- If i \neq j, then (U_i, V_i) \neq (U_j, V_j) and (U_i, V_i) \neq (V_j, U_j) (no multiple edges)
- 1 \leq W_i \leq 10^9
- W_i is an integer
- The graph is 2-edge-connected (the graph remains connected after removing any single edge)
- It is guaranteed that the answer is at most 2 \times 10^{14}
Input
N M U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- The first line contains an integer N representing the number of cities and an integer M representing the number of cables, separated by a space.
- From the 2nd line to the (M + 1)-th line, information about each cable is given over M lines.
- The (1 + i)-th line (1 \leq i \leq M) contains the city numbers U_i, V_i that the i-th cable connects, and its maintenance cost W_i, separated by spaces.
Output
Output in a single line, as an integer, the total maintenance cost of the backbone network when Aoki optimally cuts one cable and Takahashi constructs the backbone network to minimize the total maintenance cost from the remaining cables.
Sample Input 1
3 3 1 2 1 2 3 2 1 3 3
Sample Output 1
5
Sample Input 2
4 5 1 2 1 2 3 2 3 4 3 4 1 4 1 3 5
Sample Output 2
9
Sample Input 3
6 9 1 2 3 2 3 5 3 4 2 4 5 7 5 6 1 6 1 4 1 3 8 2 5 6 4 6 9
Sample Output 3
20
Sample Input 4
8 12 1 2 10 2 3 20 3 4 30 4 5 40 5 6 50 6 7 60 7 8 70 8 1 80 1 5 15 2 6 25 3 7 35 4 8 45
Sample Output 4
210
Sample Input 5
3 3 1 2 1000000000 2 3 1000000000 1 3 1000000000
Sample Output 5
2000000000