#P15043. [UOI 2022 II Stage] 图

[UOI 2022 II Stage] 图

Problem Description

The city where Xonia lives consists of nn intersections, which are connected by nn undirected roads.

The intersections are numbered from 11 to nn. The roads are also numbered from 11 to nn. The ii-th road connects intersections aia_i and bib_i, and its length is cic_i.

It is known that using the existing roads, you can travel from any intersection to any other intersection. Between any two intersections, there is at most one road. There is no road that connects an intersection to itself.

Let dist(x,y)dist(x, y) be the length of the shortest path between intersections xx and yy.

Xonia wants to find two intersections uu and vv in the city such that dist(u,v)dist(u, v) is the maximum among all possible pairs (u,v)(u, v).

Input Format

The first line contains two integers nn and gg (3≤n≤200 0003 \leq n \leq 200\,000, 0≤g≤50 \leq g \leq 5), representing the number of intersections in the city and the test group number, respectively.

The next nn lines each contain three integers aia_i, bib_i, and cic_i (1≤ai,bi≤n1 \leq a_i, b_i \leq n, 1≤ci≤1091 \leq c_i \leq 10^9).

It is guaranteed that using the roads you can travel from any intersection to any other intersection.

It is guaranteed that there is no road that connects an intersection to itself.

It is guaranteed that between any two intersections there is at most one road.

Output Format

Output the maximum value of dist(u,v)dist(u, v) over all pairs of intersections (u,v)(u, v).

4 0
1 2 1
1 3 2
2 3 3
2 4 3
6

Hint

Sample Explanation

Explanation for the first sample:

dist(1,2)=1dist(1, 2) = 1

dist(1,3)=2dist(1, 3) = 2

dist(1,4)=4dist(1, 4) = 4

dist(2,3)=3dist(2, 3) = 3

dist(2,4)=3dist(2, 4) = 3

dist(3,4)=6dist(3, 4) = 6

Therefore, the maximum dist(u,v)=6dist(u, v) = 6.

Scoring

  • (22 points): The graph structure is a simple cycle.
  • (17 points): n≤200n \leq 200.
  • (24 points): The length of each cycle in the graph does not exceed 1000.
  • (9 points): ci=1c_i = 1.
  • (28 points): No additional constraints.

Translated by DeepSeek V3.

Translated by ChatGPT 5