Losing Leaves
Time limit8sMemory limit1024 MB
Given a rooted tree and k, sell exactly k nodes closed under descendants so that the fewest leaf nodes remain in the network.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Tree
- Solved
- No attempts yet
Problem
Here at the Benelux Advanced Phone Company (BAPC), we own the largest telephone network in the Benelux area. Unfortunately, the network has grown too large to manage properly, so BAPC has decided to sell part of it.
The network consists of interconnected transmission nodes. One node is the central switch. Every other node has exactly one upstream node. Following the upstream connections from any node always leads to the central switch. A node with no downstream nodes is a leaf, called a customer transmission node.
When selling, integrity must be kept. If BAPC sells a node , it must also sell every node downstream of .
BAPC will sell exactly transmission nodes. There may be many ways to choose them. BAPC wants to make its life as easy as possible, so it wants to minimize the number of customer transmission nodes left in the network after the sale, while keeping integrity.
For example, consider the second input case, shown in Figure L.1. The three striped red nodes are sold, and the two bold green nodes are the remaining customer nodes.

Figure L.1: Visualization of the second input case.
Input
The input consists of:
- One line with two integers and (), the number of transmission nodes and the number of nodes to sell.
- lines, the th of which contains one integer (), indicating that transmission node () has an outgoing connection to node .
The transmission nodes are numbered from to , inclusive. Node is always the central switch.
Output
Output the minimum number of customer transmission nodes after selling transmission nodes. If the central switch is the only remaining node, it also counts as a customer node.