CF1593E.Gardener and Tree

普及/提高-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex.

The gardener Vitaly grew a tree from nn vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree.

Example of a tree.

For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree.

The result of applying the operation "remove all leaves" to the tree.

Note the special cases of the operation:

  • applying an operation to an empty tree (of 00 vertices) does not change it;
  • applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf);
  • applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves).

Vitaly applied kk operations sequentially to the tree. How many vertices remain?

树是无向连通图,且其中不存在环。本题讨论的是无根树。树的一个叶节点(leaf)是指至多与一个顶点相连的顶点。

园丁 Vitaly 种植了一棵包含 nn 个顶点的树。他决定对这棵树进行修剪。为此,他执行若干次操作。每次操作中,他将树的所有叶节点全部移除。

一棵树的示例。

例如,考虑上图所示的树。下图展示了对该树恰好执行一次操作后的结果。

对该树执行“移除所有叶节点”操作后的结果。

注意该操作的几种特殊情况:

  • 对空树(含 00 个顶点)执行操作,树保持不变;
  • 对仅含一个顶点的树执行操作,该顶点会被移除(该顶点被视为叶节点);
  • 对含两个顶点的树执行操作,两个顶点均被移除(两个顶点均被视为叶节点)。

Vitaly 对该树依次执行了 kk 次上述操作。问:最终还剩下多少个顶点?

输入格式

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

Each test case is preceded by an empty line.

Each test case consists of several lines. The first line of the test case contains two integers nn and kk (1≤n≤4⋅1051 \le n \le 4 \cdot 10^5, 1≤k≤2⋅1051 \le k \le 2 \cdot 10^5) — the number of vertices in the tree and the number of operations, respectively. Then n−1n - 1 lines follow, each of them contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \neq v) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.

It is guaranteed that the sum of nn from all test cases does not exceed 4⋅1054 \cdot 10^5.

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

每个测试用例之前均有一个空行。

每个测试用例由若干行组成。测试用例的第一行包含两个整数 nn 和 kk(1≤n≤4⋅1051 \le n \le 4 \cdot 10^5,1≤k≤2⋅1051 \le k \le 2 \cdot 10^5)—— 分别表示树中顶点的数量和操作的数量。接下来是 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \neq v),表示由一条边连接的一对顶点。保证所给图是一棵树,且不含环或重边。

保证所有测试用例的 nn 值之和不超过 4⋅1054 \cdot 10^5。

输出格式

For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying kk operations.

对于每个测试用例,在单独一行输出一个整数——即执行 kk 次操作后树中剩余的顶点数量。

输入输出样例

  • 输入#1

    6
    
    14 1
    1 2
    2 3
    2 4
    4 5
    4 6
    2 7
    7 8
    8 9
    8 10
    3 11
    3 12
    1 13
    13 14
    
    2 200000
    1 2
    
    3 2
    1 2
    2 3
    
    5 1
    5 1
    3 2
    2 1
    5 4
    
    6 2
    5 1
    2 5
    5 6
    4 2
    3 4
    
    7 1
    4 3
    5 1
    1 3
    6 1
    1 7
    2 1

    输出#1

    7
    0
    0
    3
    1
    2

说明/提示

The first test case is considered in the statement.

The second test case contains a tree of two vertices. 200000200000 operations are applied to it. The first one removes all two vertices, the other operations do not change the tree.

In the third test case, a tree of three vertices is given. As a result of the first operation, only 11 vertex remains in it (with the index 22), the second operation makes the tree empty.

第一个测试用例已在题目描述中给出。

第二个测试用例包含一棵两个顶点的树。对该树执行 200000200000 次操作:第一次操作移除了全部两个顶点,其余操作均不改变该树。

第三个测试用例给出了一棵三个顶点的树。在第一次操作后,树中仅剩 11 个顶点(编号为 22);第二次操作使该树变为空。

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

首页