Problem Statement

There are $N$ people, called Person $1$, Person $2$, $\ldots$, Person $N$.

The parent of Person $i$ $(2 \le i \le N)$ is Person $P_i$. Here, it is guaranteed that $P_i < i$.

How many generations away from Person $N$ is Person $1$?

Constraints

  • $2 \le N \le 50$
  • $1 \le P_i < i(2 \le i \le N)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$P_2$ $P_3$ $\dots$ $P_N$

Output

Print the answer as a positive integer.


Sample Input 1

3
1 2

Sample Output 1

2

Person $2$ is a parent of Person $3$, and thus is one generation away from Person $3$.

Person $1$ is a parent of Person $2$, and thus is two generations away from Person $3$.

Therefore, the answer is $2$.


Sample Input 2

10
1 2 3 4 5 6 7 8 9

Sample Output 2

9

从节点 $n$ 暴力往上跳就可以了。

#include<cstdio>
const int N=55;
int p[N],n,cnt;
int main()
{
scanf("%d",&n);
for(int i=2;i<=n;i++)
scanf("%d",p+i);
while(n!=1)
++cnt,n=p[n];
printf("%d",cnt);
}

[ABC263B] Ancestor的更多相关文章

  1. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  2. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  4. [LeetCode]Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  5. 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree

    题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...

  6. Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  7. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  8. leetcode 235. Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. leetcode 236. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  10. LeetCode 【235. Lowest Common Ancestor of a Binary Search Tree】

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. 12、Spring之基于xml的AOP

    阅读本文前,建议先阅读Spring之基于注解的AOP 12.1.环境搭建 创建名为spring_aop_xml的新module,过程参考9.1节 12.1.1.配置打包方式和依赖 <?xml v ...

  2. Nginx Ingress Contoller 通过 Envoy 代理和 Jaeger 进行分布式追踪(二)

    1.概述 在<应用程序通过 Envoy 代理和 Jaeger 进行分布式追踪(一)>一文中,我们详细介绍了单个应用程序如何通过 Envoy 和 Jaeger 实现链路追踪的过程.然而,单独 ...

  3. 52条SQL语句,性能优化!

    52条SQL语句,性能优化! SQL语句性能优化 1, 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2,应尽量避免在 where 子句中对 ...

  4. 使用antd-mobile遇到的坑

    在使用antd-mobile中的组件的时候,遇到了一些比较棘手的问题,经过查找相关资料和网上的帮助,在此整理出了问题以及解决办法: 在引入antd-mobile的时候,根据官网提供的文档: 1,首先安 ...

  5. 提高 MySQL查询效率的方法

    当涉及到提高MySQL查询效率时,以下是一些重要的策略和技巧,可以帮助你优化数据库性能.无论你是一个Web开发者.数据工程师还是系统管理员,这些方法都可以帮助你确保你的MySQL数据库运行得更快.更有 ...

  6. [Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)

    1 故事背景 最近项目上有个业务需求,翻译成技术需求,即:将request.headers中的几个header入参转换成request.body(pageRequest)中的内置参数. 为便于灵活配置 ...

  7. Skynet:Debug Console的扩展

    起因 最近上线服务器遇到了一些问题,上个月CPU暴涨的问题,那个经查验是死循环导致endless loop了. 这周又遇到了mem占用达到96%的问题,在debug console里调用了gc之后,跌 ...

  8. Note -「Polynomial」

    Part. 1 FFT Part. 1-1 Main 对于一个 \(n\) 次多项式 \(F(x)=\sum_{i=0}^{n}a_{i}x^{i}\),在平面直角坐标系中可以由 \(n+1\) 个点 ...

  9. Flask框架——请求扩展、flask中间件、蓝图、分析线程和协程

    文章目录 01 请求扩展 01 before_first_request :项目启动后第一次请求的时候执行 02 before_request:每次请求之前执行 03 after_request:每次 ...

  10. matlab关于阶梯图和图窗操作

    1阶梯信号绘制 Matlab 中绘制阶梯图函数:stairs x = [30 33 37 40 37 33 30 27 23 20 23 27 30 30]'; StepNum = length(x) ...