#P5536. 【XR-3】核心城市

【XR-3】核心城市

Problem Description

Country X has nn cities and n1n - 1 roads of length 11. Each road connects two cities, and any two cities can reach each other through some roads. Obviously, the cities and roads form a tree.

The king of Country X decides to designate kk cities as the core cities of Country X, and the remaining cities are non-core cities. These kk core cities must satisfy the following two conditions:

  1. These kk cities can reach each other pairwise via roads without passing through any non-core city.
  2. Define the distance from a non-core city to the kk core cities as the minimum value among its distances to the kk core cities.

To measure traffic conditions, the king invented the traffic congestion level, which is the maximum value among the distances from all non-core cities to the core cities.

The problem is: how should we choose the core cities to minimize the traffic congestion level? Output the minimum possible traffic congestion level that satisfies the conditions.

Input Format

The first line contains 22 positive integers n,kn, k.

The next n1n - 1 lines each contain 22 positive integers u,vu, v, meaning there is a road of length 11 between city uu and city vv.

Constraints:

  • 1k<n1051 \le k < n \le 10 ^ 5.
  • 1u,vn,uv1 \le u, v \le n, u \ne v, and it is guaranteed that the cities and roads form a tree.

Output Format

One line with one integer, representing the minimum traffic congestion level that satisfies the conditions.

6 3
1 2
2 3
2 4
1 5
5 6

1

Hint

[Sample Explanation]

Designate cities 1,2,51, 2, 5 as the 33 core cities. Then the distances from the other 33 non-core cities 3,4,63, 4, 6 to the core cities are all 11, so the answer is 11.

Translated by ChatGPT 5