Baltic OI '17 P6 - Cat in a tree

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 1G

Problem types
Baltic Olympiad in Informatics: 2017 Day 2, Problem 3

A cat lives in a tree that has N nodes. She will demarcate her territory by marking some of the tree nodes. Marked nodes may not be closer to each other than distance D. Find the maximum number of nodes that the cat can mark.

Input Specification

First line has two integers, N and D. The 0th node is the root node of the tree. Then follows N1 lines, the ith of which contain a single integer xi with 0xi<i (starting with i=1). This means that node xi is connected to node i.

Constraints

We always have 1N,D2×105. For subcases, the inputs have these further restrictions:

  • Group 1: 11 points N18
  • Group 2: 40 points N1500
  • Group 3: 49 points No further restrictions.

Output Specification

Output should contain one integer: the maximum number of nodes that can be marked.

Sample Input 1

Copy
4 3
0
0
1

Sample Output 1

Copy
2

Sample Input 2

Copy
3 1000
0
0

Sample Output 2

Copy
1

Comments

There are no comments at the moment.