#P6574. [BalticOI 2017] Cat in a tree

    ID: 5539 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心2017深度优先搜索,DFSBalticOI

[BalticOI 2017] Cat in a tree

Description

A kitten is on a tree with nn nodes. It marks nodes to divide its territory.
The nodes it marks must have pairwise distance at least dd.
The distance between two nodes is the number of edges on the path between them.
Find the maximum number of nodes the kitten can mark.

Input Format

The first line contains two integers: the number of nodes nn and the minimum required distance dd between marked nodes.
Node 00 is the root, and the nodes are numbered from 00 to n1n - 1.
The next n1n - 1 lines describe the edges. The ii-th line contains one number xix_i, meaning that node ii is connected to node xix_i.

Output Format

Output one integer: the maximum number of nodes the kitten can mark.

4 3
0
0
1
2
3 1000
0
0
1

Hint

Constraints and Notes

This problem uses bundled testdata.

  • Subtask 1 (11 pts): n18n \le 18.
  • Subtask 2 (40 pts): n1500n \le 1500.
  • Subtask 3 (49 pts): no special constraints.

For 100%100\% of the testdata, 1n,d2×1051 \le n, d \le 2 \times 10^5, 0xi<i0 \le x_i < i.

Explanation

Translated from BOI 2017 D2 T1 Cat in a tree.
Translator: @一只书虫仔

Translated by ChatGPT 5