#BS0000352. Cắt cây (Tree Cutting)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cắt cây (Tree Cutting)
Tree Cutting
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree on vertices numbered from to . A tree is connected and has no cycles. Exactly 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 for which exactly edges can be removed while ensuring that every remaining component contains at least vertices.
Input
- The first line contains , the number of vertices and the number of edges to remove.
- Each of the next lines contains two integers , describing an undirected edge between vertices and .
Output
Print the greatest possible value of as an integer.
Subtasks
- Subtask 1 (20 points): ; ; there are exactly edges forming an undirected tree.
- Subtask 2 (30 points): ; ; there are exactly edges forming an undirected tree.
- Subtask 3 (50 points): ; ; there are exactly edges forming an undirected tree.
Examples
Input 1
5 1
1 2
1 3
3 4
3 5
Output 1
2
Explanation
Remove edge . The components have vertices and , with sizes and . Two components of size at least would require at least vertices, exceeding . Thus the answer is .
Input 2
6 2
1 2
2 3
1 4
4 5
5 6
Output 2
2
Explanation
Remove edges and . The three components are , and , each of size . Three components of size would require more than 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 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 and , 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 and , but removing both isolates vertex . Thus size cannot be achieved.