CF1385F.Removing Leaves
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
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.
给你一棵树(即无环的连通图),该树包含 n 个顶点。这棵树是无根的——它仅仅是一个无环的无向连通图。
在一次操作中,你可以恰好选择 k 个叶子节点(叶子节点指仅与另一个顶点相连的顶点),且这些叶子节点必须全部连接到同一个顶点,然后将这些叶子节点及其关联的边一并删除。换言之,你选择叶子节点 u1,u2,…,uk,使得存在边 (u1,v)、(u2,v)、…、(uk,v),然后将这些叶子节点及对应边全部移除。
你的任务是:在最优地删除叶子节点的前提下,求出最多能执行多少次这样的操作。
你需要回答 t 个相互独立的测试用例。
输入格式
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).
输入的第一行包含一个整数 t(1≤t≤2⋅104),表示测试用例的数量。随后是 t 个测试用例。
每个测试用例的第一行包含两个整数 n 和 k(2≤n≤2⋅105;1≤k<n),分别表示树中顶点的数量以及每次操作中要删除的叶子节点数量。接下来的 n−1 行描述树的边。第 i 条边由两个整数 xi 和 yi(1≤xi,yi≤n)表示,即该边连接顶点 xi 和 yi。保证所给边集构成一棵树。
保证所有测试用例的 n 值之和不超过 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.
示例中第一个测试用例对应的图片:

在此图中,你可以在第一轮操作中移除顶点 2、5 和 3,在第二轮操作中移除顶点 1、7 和 4。
示例中第二个测试用例对应的图片:

在此图中,你可以在第一轮操作中移除顶点 7、8 和 9,第二轮操作中移除顶点 5、6 和 10,第三轮操作中移除顶点 1、3 和 4。
示例中第三个测试用例对应的图片:

在此图中,你可以在第一轮操作中移除顶点 5 和 7,第二轮操作中移除顶点 2 和 4,第三轮操作中移除顶点 1 和 6。
输入解题思路,AI测评打分。不知道怎么写?