PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000352. Cắt cây (Tree Cutting)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cắt cây (Tree Cutting)

Tree Cutting

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree on nn vertices numbered from 11 to nn. A tree is connected and has no cycles. Exactly kk edges must be removed.

After removal, the vertices form connected components. Two vertices belong to the same component if they can still be joined using the remaining edges. The size of a component is its number of vertices.

Find the greatest integer xx for which exactly kk edges can be removed while ensuring that every remaining component contains at least xx vertices.

Input

  • The first line contains n,kn,k, the number of vertices and the number of edges to remove.
  • Each of the next n−1n-1 lines contains two integers u,vu,v, describing an undirected edge between vertices uu and vv.

Output

Print the greatest possible value of xx as an integer.

Subtasks

  • Subtask 1 (20 points): 1≤k<n≤121\le k<n\le 12; 1≤u,v≤n1\le u,v\le n; there are exactly n−1n-1 edges forming an undirected tree.
  • Subtask 2 (30 points): 1≤k<n≤20001\le k<n\le 2000; 1≤u,v≤n1\le u,v\le n; there are exactly n−1n-1 edges forming an undirected tree.
  • Subtask 3 (50 points): 1≤k<n≤1000001\le k<n\le 100000; 1≤u,v≤n1\le u,v\le n; there are exactly n−1n-1 edges forming an undirected tree.

Examples

Input 1

5 1
1 2
1 3
3 4
3 5

Output 1

2

Explanation

Remove edge (1,3)(1,3). The components have vertices 1,21,2 and 3,4,53,4,5, with sizes 22 and 33. Two components of size at least 33 would require at least 66 vertices, exceeding 55. Thus the answer is 22.

Input 2

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

Output 2

2

Explanation

Remove edges (1,2)(1,2) and (4,5)(4,5). The three components are {2,3}\{2,3\}, {1,4}\{1,4\} and {5,6}\{5,6\}, each of size 22. Three components of size 33 would require more than 66 vertices.

Input 3

2 1
1 2

Output 3

1

Explanation

Remove the only edge; both components have one vertex.

Input 4

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

Output 4

3

Explanation

Removing edge (3,4)(3,4) splits the path into two groups of three vertices. Two groups of four would require eight vertices, but there are only six.

Input 5

3 1
1 2
1 3

Output 5

1

Explanation

Removing either edge isolates a leaf and leaves a component with two vertices. Three vertices cannot form two groups each of size at least two.

Input 6

8 2
1 2
1 3
2 4
2 5
3 6
3 7
3 8

Output 6

1

Explanation

Removing two leaf edges, such as (2,4)(2,4) and (2,5)(2,5), produces two singleton components and one component of six vertices. To avoid singletons, neither removed edge may touch a leaf. The only two such edges are (1,2)(1,2) and (1,3)(1,3), but removing both isolates vertex 11. Thus size 22 cannot be achieved.