CF1385F.Removing Leaves

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

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.

给你一棵树(即无环的连通图),该树包含 nn 个顶点。这棵树是无根的——它仅仅是一个无环的无向连通图。

在一次操作中,你可以恰好选择 kk 个叶子节点(叶子节点指仅与另一个顶点相连的顶点),且这些叶子节点必须全部连接到同一个顶点,然后将这些叶子节点及其关联的边一并删除。换言之,你选择叶子节点 u1,u2,…,uku_1, u_2, \dots, u_k,使得存在边 (u1,v)(u_1, v)、(u2,v)(u_2, v)、…\dots、(uk,v)(u_k, v),然后将这些叶子节点及对应边全部移除。

你的任务是:在最优地删除叶子节点的前提下,求出最多能执行多少次这样的操作。

你需要回答 tt 个相互独立的测试用例。

输入格式

The first line of the input contains one integer tt (1≤t≤2⋅1041 \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 (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5; 1≤k<n1 \le k \lt n) — the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next n−1n-1 lines describe edges. The ii-th edge is represented as two integers xix_i and yiy_i (1≤xi,yi≤n1 \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 2⋅1052 \cdot 10^5 (∑n≤2⋅105\sum n \le 2 \cdot 10^5).

输入的第一行包含一个整数 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含两个整数 nn 和 kk(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5;1≤k<n1 \le k \lt n),分别表示树中顶点的数量以及每次操作中要删除的叶子节点数量。接下来的 n−1n-1 行描述树的边。第 ii 条边由两个整数 xix_i 和 yiy_i(1≤xi,yi≤n1 \le x_i, y_i \le n)表示,即该边连接顶点 xix_i 和 yiy_i。保证所给边集构成一棵树。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5(即 ∑n≤2⋅105\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.

示例中第一个测试用例对应的图片:

在此图中,你可以在第一轮操作中移除顶点 22、55 和 33,在第二轮操作中移除顶点 11、77 和 44。

示例中第二个测试用例对应的图片:

在此图中,你可以在第一轮操作中移除顶点 77、88 和 99,第二轮操作中移除顶点 55、66 和 1010,第三轮操作中移除顶点 11、33 和 44。

示例中第三个测试用例对应的图片:

在此图中,你可以在第一轮操作中移除顶点 55 和 77,第二轮操作中移除顶点 22 和 44,第三轮操作中移除顶点 11 和 66。

输入解题思路,AI测评打分。不知道怎么写?

首页