E - Increasing Walk Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

頂点に 1 から N の番号がついた N 頂点 M 辺のグラフがあります。
i 番目の辺は頂点 U_i と頂点 V_i を結ぶ無向辺で、重みは C_i です。

このグラフにおいて、辺の重みが狭義単調増加する歩道に対する、辺の重みの総和の最大値を求めてください。

T 個のケースが与えられるので、それぞれについて答えを求めてください。

歩道とは 頂点と辺を交互に並べた列 (v_0,e_1,v_1,e_2,\dots,e_k,v_k) であって、以下の条件を満たすものを歩道という。
  • 全ての i について、 e_i は v_{i-1} と v_i を結ぶ

制約

  • T \geq 1
  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1 \leq U_i < V_i \leq N
  • 1 \leq C_i \leq 10^9
  • (U_i,V_i) は相異なる
  • T 個のケースにわたる N の総和は 2\times 10^5 以下
  • T 個のケースにわたる M の総和は 2\times 10^5 以下
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N M
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M

出力

T 行出力せよ。 i 行目には i 番目のテストケースに対する答えを出力せよ。


入力例 1

2
6 9
1 2 10
1 3 10
2 3 20
2 4 40
2 5 30
3 5 40
3 6 50
4 5 50
5 6 50
4 6
1 3 1
3 4 1
2 3 1
1 2 1
2 4 1
1 4 1

出力例 1

150
1

1 番目のテストケースについて、下図のとおり 1 \to 3 \to 2 \to 5 \to 3 \to 6 と移動する歩道に含まれる辺の重みの総和は 10+20+30+40+50=150 です。

辺の重みが狭義単調増加する歩道であって、重みの総和がこれより大きくなるものは存在しないため、答えは 150 となります。

図

Score : 450 points

Problem Statement

There is a graph with N vertices and M edges, where the vertices are numbered 1 to N.
The i-th edge is an undirected edge connecting vertex U_i and vertex V_i, with weight C_i.

Find the maximum total edge weight of a walk in this graph along which the edge weights are strictly increasing.

You are given T cases; solve each of them.

What is a walk? A sequence (v_0,e_1,v_1,e_2,\dots,e_k,v_k) in which vertices and edges alternate is called a walk if it satisfies the following condition.
  • e_i connects v_{i-1} and v_i for every i.

Constraints

  • T \geq 1
  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1 \leq U_i < V_i \leq N
  • 1 \leq C_i \leq 10^9
  • The pairs (U_i,V_i) are pairwise distinct.
  • The sum of N over the T cases is at most 2\times 10^5.
  • The sum of M over the T cases is at most 2\times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N M
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M

Output

Print T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

2
6 9
1 2 10
1 3 10
2 3 20
2 4 40
2 5 30
3 5 40
3 6 50
4 5 50
5 6 50
4 6
1 3 1
3 4 1
2 3 1
1 2 1
2 4 1
1 4 1

Sample Output 1

150
1

For the 1-st test case, as shown in the figure below, the walk 1 \to 3 \to 2 \to 5 \to 3 \to 6 has a total edge weight of 10+20+30+40+50=150.

No walk along which the edge weights are strictly increasing has a larger total weight, so the answer is 150.

Figure