#P1272. 重建道路

重建道路

Description

After a terrible earthquake, people rebuilt Farmer John's ranch with NN barns (numbered 1N1 \sim N). Since there was no time to build extra roads, there is a unique path between any two barns. Therefore, the ranch road network forms a tree.

John wants to know how severe the damage would be in another earthquake. Some roads, once destroyed, would separate a subtree containing PP barns from the remaining barns. John wants to know the minimum number of such roads.

Input Format

The first line contains two integers, NN and PP.

From the second line to the NN-th line (i.e., N1N - 1 lines), each line contains two integers II and JJ, indicating that node II is the parent of node JJ. The road network can be built as a tree rooted at node 11.

Output Format

Output a single line containing the minimum number of roads whose destruction would separate a subtree with exactly PP nodes.

11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11

2

Hint

Sample explanation: If roads 141-4 and 151-5 are destroyed, the subtree containing nodes (1,2,3,6,7,81, 2, 3, 6, 7, 8) will be separated.

Constraints: 1N1501 \le N \le 150, 1PN1 \le P \le N. It is guaranteed that the input forms a tree.

Translated by ChatGPT 5