codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1593E.md (2942B)


      1 Source: https://codeforces.com/problemset/problem/1593/E
      2 
      3 E. Gardener and Tree
      4 
      5 time limit per test
      6 
      7 4 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 The gardener Vitaly grew a tree from $n$ 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.
     24 
     25  ![](https://espresso.codeforces.com/390f0f2fee2dccde0befa41c992fa715c0864037.png) Example of a tree. 
     26 
     27 For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree.
     28 
     29  ![](https://espresso.codeforces.com/3e241ec4bc248a37748afb14103a67d47298e571.png) The result of applying the operation "remove all leaves" to the tree. 
     30 
     31 Note the special cases of the operation:
     32 
     33 * applying an operation to an empty tree (of $0$ vertices) does not change it;
     34 * applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf);
     35 * applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves).
     36 
     37 Vitaly applied $k$ operations sequentially to the tree. How many vertices remain?
     38 
     39 Input
     40 
     41 The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
     42 
     43 Each test case is preceded by an empty line.
     44 
     45 Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \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.
     46 
     47 It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$.
     48 
     49 Output
     50 
     51 For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations.
     52 
     53 Example
     54 
     55 Input
     56 
     57 ```
     58 6
     59 
     60 14 1
     61 1 2
     62 2 3
     63 2 4
     64 4 5
     65 4 6
     66 2 7
     67 7 8
     68 8 9
     69 8 10
     70 3 11
     71 3 12
     72 1 13
     73 13 14
     74 
     75 2 200000
     76 1 2
     77 
     78 3 2
     79 1 2
     80 2 3
     81 
     82 5 1
     83 5 1
     84 3 2
     85 2 1
     86 5 4
     87 
     88 6 2
     89 5 1
     90 2 5
     91 5 6
     92 4 2
     93 3 4
     94 
     95 7 1
     96 4 3
     97 5 1
     98 1 3
     99 6 1
    100 1 7
    101 2 1
    102 ```
    103 
    104 Output
    105 
    106 ```
    107 7
    108 0
    109 0
    110 3
    111 1
    112 2
    113 ```
    114 
    115 Note
    116 
    117 The first test case is considered in the statement.
    118 
    119 The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree.
    120 
    121 In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.