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. Tarjan基础用法

    \(\operatorname{Tarjan}\) 基础用法 目录 \(\operatorname{Tarjan}\) 基础用法 \(\operatorname{Tarjan}\) 求最近公共祖先 前 ...

  2. Go 并发编程 - Goroutine 基础 (一)

    基础概念 进程与线程 进程是一次程序在操作系统执行的过程,需要消耗一定的CPU.时间.内存.IO等.每个进程都拥有着独立的内存空间和系统资源.进程之间的内存是不共享的.通常需要使用 IPC 机制进行数 ...

  3. vsftp简单配置

    添加用户 vi /etc/pam.d/vsftdp 注释掉 auth required pam_shells.so

  4. centos8环境基本优化

    centos8环境基本优化 目录 centos8环境基本优化 1.防火墙优化 2.源优化: 方案1.更换阿里源 方案2.使用centos8.5 源 安装epel源 3.ssh连接慢解决 4.关闭公网, ...

  5. Codeforces Round div.2 C

    Smiling & Weeping ----我对姑娘的喜欢,何止钟意二字 题目链接:Problem - C - Codeforces 自我分析:我感觉这是一道很有意义的题目,可以帮我们更好的理 ...

  6. Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例

    Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例 点击封面跳转到Unity国际版下载页面 简介 在Unity中, ...

  7. P251——用RadialGradientBrush填充椭圆,并进行RotateTransform变换

    一.认识RadialGradientBrush(径向渐变) 1.坐标 RadialGradientBrush可以用来填充矩形(正方形)和椭圆(正圆), 填充区域使用比例坐标, 椭圆的坐标(0,0)和( ...

  8. 教育法学第八章单元测试MOOC

    第八章单元测试 返回 本次得分为:100.00/100.00, 本次测试的提交时间为:2020-09-06, 如果你认为本次测试成绩不理想,你可以选择 再做一次 . 1 单选(5分) 社团法人与财团法 ...

  9. Go 复合类型之切片类型介绍

    Go 复合类型之切片类型 目录 Go 复合类型之切片类型 一.引入 二.切片(Slice)概述 2.1 基本介绍 2.2 特点 2.3 切片与数组的区别 三. 切片声明与初始化 3.1 方式一:使用切 ...

  10. 深入理解 python 虚拟机:GIL 源码分析——天使还是魔鬼?

    深入理解 python 虚拟机:GIL 源码分析--天使还是魔鬼? 在目前的 CPython 当中一直有一个臭名昭著的问题就是 GIL (Global Interpreter Lock ),就是全局解 ...