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: Wrong Answer)

using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
using System.Text;//テキストの高速出力に必要
class Program
{
static string[] input = Console.ReadLine().Split(' ');
static int n = int.Parse(input[0]);
static int k = int.Parse(input[1]);
static List<int>[] nextList = new List<int>[n];
static List<int>[] childrenList = new List<int>[n];
static int[] depths = new int[n];//木の深さ
static List<int[]> changeVertexList = new List<int[]>();//深さを変えるべき頂点の一覧
static int[] parents = new int[n];

	static void Main()
	{
    long answer = 0;
    int[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse);
    for(int i = 0; i < n; i++)
    {
      nextList[i] = new List<int>();
      childrenList[i] = new List<int>();
    }
    
    for(int i = 0; i < n-1; i++)
    {
      nextList[i+1].Add(nums[i+1]-1);
      nextList[nums[i+1]-1].Add(i+1);
    }

    MakeChildrenList(0);

    parents = VertexParents();
    SearchDepths(0,0);
    
    for(int i = 0; i < n-1; i++)
    {
      if(depths[i+1] > k)
      {
        changeVertexList.Add(new int[2]{i+1,depths[i+1]});
      }
    }
    changeVertexList.Sort((a, b) => b[1].CompareTo(a[1]));//深い頂点から

    for(int i = 0; i < changeVertexList.Count(); i++)
    {
      if(depths[changeVertexList[i][0]] > k)
      {
        VertexRemove(depths[changeVertexList[i][0]],k-1);
        answer++;
      }
    }

    bool allOne = true;
    for(int i = 0; i < n-1; i++)
    {
      if(nums[i+1] != 1) allOne = false;
    }

    if(nums[0] != 1)
    {
      if(allOne && k%2 == 1) answer += 0;
      else answer++;
    }
 
    Console.WriteLine(answer);
	}

  static void VertexRemove(int vertexNum, int parentCount)
  {
    depths[vertexNum] = 0;
    if(parentCount > 0 && parents[vertexNum] != 0)
    {
      VertexRemove(parents[vertexNum],parentCount-1);
    }
    
    for(int i = 0; i < childrenList[vertexNum].Count(); i++)
    {
      VertexRemove(childrenList[vertexNum][i],0);
    }
  }

  static void MakeChildrenList(int rootNum)
  {//nextListからchildrenListを、引数を0-indexの根の番号としてO(頂点数)で作る
    for(int i = 0; i < nextList[rootNum].Count(); i++)
    {
      childrenList[rootNum].Add(nextList[rootNum][i]);
      nextList[nextList[rootNum][i]].Remove(rootNum);//木の逆走を防ぐ
      MakeChildrenList(nextList[rootNum][i]);
    }
  }

  static int[] VertexParents()
  {//頂点数nとchildrenListから、各頂点の親（根は-1）をO(n)で返す
    int[] answers = new int[n];
    for(int i = 0; i < n; i++) answers[i] = -1;

    for(int i = 0; i < n; i++)
    {
      for(int j = 0; j < childrenList[i].Count; j++)
      {
        answers[childrenList[i][j]] = i;
      }
    }

    return answers;
  }

  static void SearchDepths(int vertexNum, int depth)
  {//頂点数nとchildrenListから、引数の頂点の深さ及び、その子の深さをO(頂点数)で設定
    depths[vertexNum] = depth;//深さを設定

    for(int i = 0; i < childrenList[vertexNum].Count; i++)
    {
      SearchDepths(childrenList[vertexNum][i],depth+1);//子の深さを設定
    }
  }

}