KK Subway Stations

시간 제한1.5초메모리 제한1024 MB

요약
가중치가 있는 트리에서 노드 K개 이하의 단순 경로를 골라, 모든 노드에서 가장 가까운 선택 노드까지의 거리 최댓값을 최소화한다.
난이도

어려움10점 중 9점

유형
이분 탐색, 트리, DFS, 그리디
정답자
아직 제출이 없습니다

문제

Seoul City Mayor Hanbyeol plans to build a subway system to alleviate traffic congestion on its jam-packed roads. However, due to budget constraints, only a maximum of KK stations can be built, and a single path of roads must connect all the stations.

Specifically, when the layout of Seoul can be represented as a connected graph with NN nodes and N−1N-1 bidirectional edges, where a node represents a building and an edge represents a road, Hanbyeol must select a simple path of XX nodes, where 1≤X≤K1 \le X \le K, and build stations on every node along that path.

Hanbyeol wants to minimize the maximum distance between the buildings to the nearest station. To help Hanbyeol, write a program that determines the best location for the subway stations so that such value is minimized.

입력

The first line of input contains two space-separated integers: NN, denoting the count of the nodes in the city, and KK, denoting the count of the maximum subway stations that Hanbyeol can build. (1≤K≤N≤300,0001 \le K \le N \le 300\\,000)

The ii-th of the next N−1N-1 lines of input contains three space-separated integers: u_iu\_i and v_iv\_i, denoting the indices of nodes that the ii-th road is connecting, and l_il\_i, denoting the length of the ii-th road in kilometers. (1≤u_i,v_i≤N;1 \le u\_i, v\_i \le N; 1≤l_i≤1012;1 \le l\_i \le 10^{12}; i≠j→u_i,v_i≠u_j,v_ji \ne j \rightarrow \\{u\_i,v\_i\\} \ne \\{u\_j,v\_j\\})

출력

Output the maximum distance from any building to the nearest subway station when it is the minimum possible, in kilometers.

힌트

(For Sogang students:) Note that this problem is an improvised version that matches the format of a problem in a general programming contest. While in the exam, the original scoring was:

  • 5555 points for Subtask 1.
  • 4040 points for Subtask 2.
  • 33 points for Subtask 3.
  • 22 points for Subtask 4.

예제2

  1. 예제 1

    입력
    9 3
    1 2 5
    2 3 8
    3 4 6
    3 5 9
    5 6 8
    5 9 11
    6 7 1
    6 8 12
    
    예상 출력
    13
    
  2. 예제 2

    입력
    9 5
    1 2 5
    2 3 8
    3 4 6
    3 5 9
    5 6 8
    5 9 11
    6 7 1
    6 8 12
    
    예상 출력
    11