CF1918F.Caterpillar on a Tree
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
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 1. Each crawl to a neighboring node takes 1 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 0 seconds. But the trampoline is old and can withstand no more than k 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 1) and moves using two methods.
- Crawl along an edge to one of the neighboring nodes: takes 1 minute.
- Teleport to the root: takes no time, no new nodes become visited.
The second method (teleportation) can be used at most k times. The caterpillar can finish the journey at any node.
毛毛虫决定访问树中的每个节点。最初,它位于根节点。
该树表示为一棵以节点 1 为根的有根树。毛毛虫每次爬行至相邻节点需耗时 1 分钟。
树下有一张蹦床。若毛毛虫从树上脱离并坠落到蹦床上,它将瞬间回到树的根节点(耗时 0 秒)。但该蹦床已老化,最多只能承受 k 次毛毛虫的坠落。
毛毛虫访问树中所有节点所需的最短时间是多少?
更形式化地,我们需要求出:在以下条件下,毛毛虫访问树中所有节点所需的最短时间:
- 毛毛虫起始于根节点(节点 1);
- 它可通过两种方式移动:
- 沿一条边爬行至某个相邻节点:耗时 1 分钟;
- 瞬间传送到根节点:耗时 0 秒,且传送不会使任何新节点被访问。
第二种方式(传送)最多可使用 k 次。毛毛虫可在任意节点结束整个旅程。
输入格式
The first line of the input contains two integers: n (2≤n≤2⋅105) — the number of nodes in the tree, and k (0≤k≤109) — the maximum number of teleports to the root.
The second line contains n−1 integers p2, p3, ..., pn (1≤pi≤n) — the ancestors in the tree for nodes 2,3,…,n; node 1 is the root.
输入的第一行包含两个整数:n(2≤n≤2⋅105)——树中节点的数量,以及 k(0≤k≤109)——允许向根节点传送的最大次数。
第二行包含 n−1 个整数 p2, p3, ..., pn(1≤pi≤n)——节点 2,3,…,n 在树中的父节点;节点 1 为根节点。
输出格式
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.

图中展示了一种在9分钟内遍历第一次测试中树的方式。

输入解题思路,AI测评打分。不知道怎么写?