Under construction

Still being built. Things may change

Losing Leaves

Time limit8sMemory limit1024 MB

Summary
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 XX, it must also sell every node downstream of XX.

BAPC will sell exactly kk 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 nn and kk (0≤k<n≤1060\leq k < n\leq 10^6), the number of transmission nodes and the number of nodes to sell.
  • n−1n-1 lines, the iith of which contains one integer pip_i (0≤pi<i0 \leq p_i < i), indicating that transmission node ii (1≤i<n1 \leq i < n) has an outgoing connection to node pip_i.

The transmission nodes are numbered from 00 to n−1n - 1, inclusive. Node 00 is always the central switch.

Output

Output the minimum number of customer transmission nodes after selling kk transmission nodes. If the central switch is the only remaining node, it also counts as a customer node.

Examples2

  1. Example 1

    Input
    5 2
    0
    0
    1
    1
    
    Expected output
    1
    
  2. Example 2

    Input
    9 3
    0
    0
    1
    1
    1
    4
    5
    6
    
    Expected output
    2