CF1918F.Caterpillar on a Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The caterpillar decided to visit every node of the tree. Initially, it is sitting at the root.

The tree is represented as a rooted tree with the root at the node 11 . Each crawl to a neighboring node takes 11 minute for the caterpillar.

And there is a trampoline under the tree. If the caterpillar detaches from the tree and falls onto the trampoline, it will end up at the root of the tree in 00 seconds. But the trampoline is old and can withstand no more than kk caterpillar's falls.

What is the minimum time the caterpillar can take to visit all the nodes of the tree?

More formally, we need to find the minimum time required to visit all the nodes of the tree, if the caterpillar starts at the root (node 11 ) and moves using two methods.

  1. Crawl along an edge to one of the neighboring nodes: takes 11 minute.
  2. Teleport to the root: takes no time, no new nodes become visited.

The second method (teleportation) can be used at most kk times. The caterpillar can finish the journey at any node.

输入格式

The first line of the input contains two integers: nn ( 2n21052 \le n \le 2\cdot 10^5 ) — the number of nodes in the tree, and kk ( 0k1090 \le k \le 10^9 ) — the maximum number of teleports to the root.

The second line contains n1n-1 integers p2p_2 , p3p_3 , ..., pnp_n ( 1pin1 \le p_i \le n ) — the ancestors in the tree for nodes 2,3,,n2, 3, \ldots, n ; node 11 is the root.

输出格式

Print a single number in a single line — the minimum number of minutes required to visit all the nodes of the tree.

输入输出样例

  • 输入#1

    8 1
    1 1 2 2 4 3 3

    输出#1

    9
  • 输入#2

    4 0
    4 4 1

    输出#2

    4

说明/提示

The figure shows one of the ways to traverse the tree from the first test in 9 minutes.

首页