K Network Stations

면접 대비

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

요약
가중치 트리를 K개의 연결된 영역으로 나눌 때 각 영역 내 모든 건물 쌍의 거리 합의 최댓값을 최소로 만드는 값을 구한다.
난이도

어려움10점 중 8점

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

문제

The city of “문해프” consists of NN buildings and N−1N - 1 roads, forming a tree structure (meaning that every pair of buildings is connected by exactly one simple path, and there are no cycles.). All roads are bidirectional and have their own lengths.

The newly appointed mayor of “문해프” City plans to divide the city into KK regions by removing K−1K-1 roads. (if K=1K = 1, a single region covers the entire city.) Then, one Network Station will be built in each region to facilitate communication among the buildings within that region.

Each region RR contains multiple buildings. The communication complexity of region RR is defined as ∑_i,j∈R,i\<jdist⁡(i,j)\sum\_{i,j\in R, i\<j} \operatorname{dist}(i,j), where the sum runs over all unordered pairs of distinct buildings in RR. In other words, the communication complexity of a region is the total sum of distances between all pairs of buildings in that region.

The mayor of “문해프” City wants to minimize the maximum communication complexity among all regions. Find the minimum possible value of this maximum communication complexity when the city is divided optimally.

입력

The first line contains two integers NN and KK, the number of buildings and the number of regions to divide the city into. (K≤N≤100,000,K∈1,2)(K \leq N \leq 100\\,000, K \in \\{1,2\\})

Each of the next N−1N-1 lines contains three integers a_ia\_i, b_ib\_i, and c_ic\_i, indicating that there is a bidirectional road between buildings a_ia\_i and b_ib\_i with length c_ic\_i. (1≤c_i≤100)(1 \leq c\_i \leq 100)

It is guaranteed that the given graph forms a tree.

출력

Print a single integer: the minimum possible value of the maximum communication complexity among all regions.

예제3

  1. 예제 1

    입력
    4 1
    2 1 1
    2 3 2
    2 4 4
    
    예상 출력
    21
    
  2. 예제 2

    입력
    5 2
    1 2 1
    1 3 2
    1 4 10
    4 5 3
    
    예상 출력
    6
    
  3. 예제 3

    입력
    1 1
    
    예상 출력
    0