CF1065F.Up and Down the Tree

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree with nn vertices; its root is vertex 11. Also there is a token, initially placed in the root. You can move the token to other vertices. Let's assume current vertex of token is vv, then you make any of the following two possible moves:

  • move down to any leaf in subtree of vv;
  • if vertex vv is a leaf, then move up to the parent no more than kk times. In other words, if h(v)h(v) is the depth of vertex vv (the depth of the root is 00), then you can move to vertex toto such that toto is an ancestor of vv and h(v)−k≤h(to)h(v) - k \le h(to).

Consider that root is not a leaf (even if its degree is 11). Calculate the maximum number of different leaves you can visit during one sequence of moves.

给你一棵包含 nn 个顶点的树,其根节点为顶点 11。此外还有一个令牌,初始时位于根节点。你可以将令牌移动到其他顶点。假设令牌当前位于顶点 vv,则你可以执行以下两种操作之一:

  • 向下移动至 vv 的子树中的任意一个叶节点;
  • 若顶点 vv 是叶节点,则最多向上移动至其祖先节点 kk 次。换言之,若 h(v)h(v) 表示顶点 vv 的深度(根节点深度为 00),则你可以移动到某个祖先节点 toto,满足 h(v)−k≤h(to)h(v) - k \le h(to)。

注意:根节点不被视为叶节点(即使其度数为 11)。请计算在一次移动序列中,最多可以访问多少个互不相同的叶节点。

输入格式

The first line contains two integers nn and kk (1≤k<n≤1061 \le k \lt n \le 10^6) — the number of vertices in the tree and the restriction on moving up, respectively.

The second line contains n−1n - 1 integers p2,p3,…,pnp_2, p_3, \dots, p_n, where pip_i is the parent of vertex ii.

It is guaranteed that the input represents a valid tree, rooted at 11.

第一行包含两个整数 nn 和 kk(1≤k<n≤1061 \le k \lt n \le 10^6)—— 分别表示树中顶点的数量以及向上移动的限制。

第二行包含 n−1n - 1 个整数 p2,p3,…,pnp_2, p_3, \dots, p_n,其中 pip_i 表示顶点 ii 的父节点。

保证输入数据构成一棵以 11 为根的合法树。

输出格式

Print one integer — the maximum possible number of different leaves you can visit.

输出一个整数——你能访问的不同叶子节点的最大可能数量。

输入输出样例

  • 输入#1

    7 1
    1 1 3 3 4 4

    输出#1

    4
  • 输入#2

    8 2
    1 1 2 3 4 5 5

    输出#2

    2

说明/提示

The graph from the first example:

One of the optimal ways is the next one: 1→2→1→5→3→7→4→61 \rightarrow 2 \rightarrow 1 \rightarrow 5 \rightarrow 3 \rightarrow 7 \rightarrow 4 \rightarrow 6.

The graph from the second example:

One of the optimal ways is the next one: 1→7→5→81 \rightarrow 7 \rightarrow 5 \rightarrow 8. Note that there is no way to move from 66 to 77 or 88 and vice versa.

第一个示例的图:

其中一种最优路径如下:1→2→1→5→3→7→4→61 \rightarrow 2 \rightarrow 1 \rightarrow 5 \rightarrow 3 \rightarrow 7 \rightarrow 4 \rightarrow 6。

第二个示例的图:

其中一种最优路径如下:1→7→5→81 \rightarrow 7 \rightarrow 5 \rightarrow 8。注意,无法在 66 与 77 或 88 之间相互移动。

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

首页