Problem:
Score : 800 points

Problem Statement

There are N towns in Snuke Kingdom, conveniently numbered 1 through N.
Town 1 is the capital.

Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place.
The destination of the Teleporter of town i is town a_i (1≤a_i≤N).
It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.

King Snuke loves the integer K.
The selfish king wants to change the destination of the Teleporters so that the following holds:

Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.

Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.

Constraints

2≤N≤10^5

1≤a_i≤N

One can get to the capital from any town by using the Teleporters some number of times.

1≤K≤10^9

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.

Sample Input 1

3 1
2 3 1

Sample Output 1

2

Change the destinations of the Teleporters to a = (1,1,1).

Sample Input 2

4 2
1 1 2 2

Sample Output 2

0

There is no need to change the destinations of the Teleporters, since the king's desire is already satisfied.

Sample Input 3

8 2
4 1 2 3 1 2 3 4

Sample Output 3

3

For example, change the destinations of the Teleporters to a = (1,1,2,1,1,2,2,4).

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
class Program
{       
    static void MakeDepth(int[] depth, int index, int dtemp, List<int>[] children, int maxDepth)
    {
        // depth[index] = dtemp;
        // foreach(int i in children[index])
        // {
        //     MakeDepth(depth, i, dtemp + 1, children);
        // }
        var que = new Queue<int>();
        que.Enqueue(index);
        int count = dtemp;
        while(que.Any() && maxDepth >= count)
        {
            int queCount = que.Count;
            foreach(int i in Enumerable.Range(0, queCount))
            {
                int p = que.Dequeue();
                depth[p] = count;
                foreach(int child in children[p])
                    que.Enqueue(child);
            }
            count++;
        }
    }
    static int GetParent(int[] parents, int index, int depth)
    {
        // if(depth == 0)
        //     return index;
        // return GetParent(parents, parents[index], depth - 1);
        while(depth > 0)
        {
            index = parents[index];
            depth--;
        }
        return index;
    }
    public void Slove()
    {
        var inputs = Console.ReadLine().Split();
        int n = int.Parse(inputs[0]);
        int k = int.Parse(inputs[1]);
        var children =
            Enumerable.Range(0, n)
            .Select(x => new List<int>())
            .ToArray();
        var parents = Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray();
        int result = 0;
        if(parents[0] != 0)
        {
            parents[0] = 0;
            result++;
        }
        foreach(var item in parents.Select((x, i) => new {Parent = x, Child = i}).Skip(1))
        {
            children[item.Parent].Add(item.Child); 
        }
        int[] depth = new int[n];
        MakeDepth(depth, 0, 0, children, n);
        foreach(int i in Enumerable.Range(0, n).OrderByDescending(x => depth[x]))
        {
            if(depth[i] > k)
            {
                int parent = GetParent(parents, i, k - 1);
                MakeDepth(depth, parent, 1, children, k);
                result++;
            }
        }
        System.Console.WriteLine(result);
    }
    
    static void Main(string[] args)
    {
        new Program().Slove();
    }
}