CF1385F.Removing Leaves
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree (connected graph without cycles) consisting of n vertices. The tree is unrooted — it is just a connected undirected graph without cycles.
In one move, you can choose exactly k leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves u1,u2,…,uk that there are edges (u1,v) , (u2,v) , … , (uk,v) and remove these leaves and these edges.
Your task is to find the maximum number of moves you can perform if you remove leaves optimally.
You have to answer t independent test cases.
输入格式
The first line of the input contains one integer t ( 1≤t≤2⋅104 ) — the number of test cases. Then t test cases follow.
The first line of the test case contains two integers n and k ( 2≤n≤2⋅105 ; 1≤k<n ) — the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next n−1 lines describe edges. The i -th edge is represented as two integers xi and yi ( 1≤xi,yi≤n ), where xi and yi are vertices the i -th edge connects. It is guaranteed that the given set of edges forms a tree.
It is guaranteed that the sum of n does not exceed 2⋅105 ( ∑n≤2⋅105 ).
输出格式
For each test case, print the answer — the maximum number of moves you can perform if you remove leaves optimally.
输入输出样例
输入#1
4 8 3 1 2 1 5 7 6 6 8 3 1 6 4 6 1 10 3 1 2 1 10 2 3 1 5 1 6 2 4 7 10 10 9 8 10 7 2 3 1 4 5 3 6 7 4 1 2 1 4 5 1 1 2 2 3 4 3 5 3
输出#1
2 3 3 4
说明/提示
The picture corresponding to the first test case of the example:
There you can remove vertices 2 , 5 and 3 during the first move and vertices 1 , 7 and 4 during the second move.
The picture corresponding to the second test case of the example:
There you can remove vertices 7 , 8 and 9 during the first move, then vertices 5 , 6 and 10 during the second move and vertices 1 , 3 and 4 during the third move.
The picture corresponding to the third test case of the example:
There you can remove vertices 5 and 7 during the first move, then vertices 2 and 4 during the second move and vertices 1 and 6 during the third move.