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 long[] distanceMemos = new long[2];//キューから捨てた最短距離と頂点番号

  static void Main()
  {
    string[] input = Console.ReadLine().Split(' ');
    int n = int.Parse(input[0]);
    int e = n-1;
    int k = int.Parse(input[1]);
    long[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);
    long[,] edges = new long[e,3];
    long answer = 0;
    bool allOne = true;

    for(long i = 0; i < n-1; i++)
    {
      edges[i,0] = nums[i+1]-1;//有向辺でないなら2本必要
      edges[i,1] = i+1;
      edges[i,2] = 1;
    }
     
    long[] distances = Dijkstra(n,e,0,edges);
    for(int i = 0; i < n-1; i++)
    {
      if(distances[i+1] > k) answer++;
      if(distances[i+1] > 1) allOne = false;
    }

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

    Console.WriteLine(answer);
  }
 
  static long[] Dijkstra(int vertexCount, int edgeCount, int startV, long[,] edges)
  {//ダイクストラ法により、O((|頂点数|+|辺数|)log頂点数)で各頂点への最短距離を求める
  //引数は頂点数、辺数、開始頂点(0-index)、辺の始点と終点と距離
  //返り値は各頂点までの最短距離、ただし到達できない場合は-1

    int n = vertexCount;
    List<long[]>[] nextList = new List<long[]>[n];//頂点番号ごとの隣接リスト。距離、隣接頂点番号
    for(long i = 0; i < n; i++)
    {
      nextList[i] = new List<long[]>();//リストの宣言が必要
    }
    long[] colors = new long[n];//最短距離未確定の頂点が0、確定で2
    long[] distances = new long[n];//各頂点への最短距離

    for(long i = 0; i < edgeCount; i++)
    {
      long[] nextMemo = new long[2];
      nextMemo[1] = edges[i,1];//隣接頂点番号
      nextMemo[0] = edges[i,2];//距離
      nextList[edges[i,0]].Add(nextMemo);//隣接リストの作成
    }
  
    List<long[]> distanceList = new List<long[]>();//優先度付きキュー
    for(long i = 0; i < n; i++)
    {
      distances[i] = long.MaxValue;//最短距離を無限とおく
    }
 
    long[] newDistance = new long[2];
 
    //スタート地点を決める
    newDistance[1] = startV;//スタート地点番号
    distances[startV] = 0;
    colors[startV] = 1;
 
    newDistance[0] = 0;//スタートからスタートまでの距離0
    distanceList.Add(newDistance);
 
    while(distanceList.Count() != 0)
    {
      distanceList = PopHeap(distanceList);
      long minVertex = distanceMemos[1];//最短頂点の番号
      colors[minVertex] = 2;
      if(distances[minVertex] < distanceMemos[0]) continue;//最小値が最短でないなら無視
 
      for(long i = 0; i < nextList[minVertex].Count(); i++)
      {
        long v = nextList[minVertex][(int)i][1];
        if(colors[v] == 2) continue;
        if(distances[v] > distances[minVertex] + nextList[minVertex][(int)i][0])
        {
          distances[v] = distances[minVertex] + nextList[minVertex][(int)i][0];
          long[] newDistanceSub = new long[2];
          newDistanceSub[0] = distances[v];
          newDistanceSub[1] = v;
          distanceList = PushHeap(distanceList, newDistanceSub);
          colors[v] = 1;
        }
      }
    }

    for(long i = 0; i < n; i++)
    {
      if(distances[i] == long.MaxValue) distances[i] = -1;
        //全頂点について最短距離の出力。到達不可能な頂点は-1を出力。
    }
    return distances;
  }
 
  //引数は、昇順にソート済みのリスト
  static List<long[]> PushHeap(List<long[]> nums, long[] addNums)//現在のヒープnumsにaddNumを追加
  {
    int n = nums.Count;
    nums.Add(addNums);
 
    while (n > 0)
    {
      int i = (n-1)/2;
      if (nums[n][0] < nums[i][0])//親のほうが大きいなら入れ替え。
      {
        long tmp = nums[n][0];
        long tmpSub = nums[n][1];
        nums[n][0] = nums[i][0];
        nums[n][1] = nums[i][1];
        nums[i][0] = tmp;
        nums[i][1] = tmpSub;
      }
      n = i;
    }
    return nums;
  }
 
  static List<long[]> PopHeap(List<long[]> nums)//ヒープnumsからポップ
  {
    int n = nums.Count - 1;
    distanceMemos[0] = nums[0][0];//捨てる値の保存
    distanceMemos[1] = nums[0][1];
    nums[0][0] = nums[n][0];//最小値である根を削除してリストの最後を持ってくる
    nums[0][1] = nums[n][1];
    nums.RemoveAt(n);
 
    for (int i = 0, j; (j = 2*i+1) < n; )
    {
      if ((j != n-1) && (nums[j][0] > nums[j+1][0]))// ２つの子のうち、値の小さい方を選ぶ
        j++;
      if (nums[i][0] > nums[j][0])//子が親より小さいなら入れ替え
      {
        long tmp = nums[j][0];
        long tmpSub = nums[j][1];
        nums[j][0] = nums[i][0];
        nums[j][1] = nums[i][1];
        nums[i][0] = tmp;
        nums[i][1] = tmpSub;
      }
      i = j;
    }
    return nums;
  }
   
}