CF1385F.Removing Leaves

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree (connected graph without cycles) consisting of nn vertices. The tree is unrooted — it is just a connected undirected graph without cycles.

In one move, you can choose exactly kk 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,,uku_1, u_2, \dots, u_k that there are edges (u1,v)(u_1, v) , (u2,v)(u_2, v) , \dots , (uk,v)(u_k, 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 tt independent test cases.

输入格式

The first line of the input contains one integer tt ( 1t21041 \le t \le 2 \cdot 10^4 ) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and kk ( 2n21052 \le n \le 2 \cdot 10^5 ; 1k<n1 \le k < n ) — the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next n1n-1 lines describe edges. The ii -th edge is represented as two integers xix_i and yiy_i ( 1xi,yin1 \le x_i, y_i \le n ), where xix_i and yiy_i are vertices the ii -th edge connects. It is guaranteed that the given set of edges forms a tree.

It is guaranteed that the sum of nn does not exceed 21052 \cdot 10^5 ( n2105\sum n \le 2 \cdot 10^5 ).

输出格式

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 22 , 55 and 33 during the first move and vertices 11 , 77 and 44 during the second move.

The picture corresponding to the second test case of the example:

There you can remove vertices 77 , 88 and 99 during the first move, then vertices 55 , 66 and 1010 during the second move and vertices 11 , 33 and 44 during the third move.

The picture corresponding to the third test case of the example:

There you can remove vertices 55 and 77 during the first move, then vertices 22 and 44 during the second move and vertices 11 and 66 during the third move.

首页