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. k8s发布应用

    前言 首先以SpringBoot应用为例介绍一下k8s的发布步骤. 1.从代码仓库下载代码,比如GitLab: 2.接着是进行打包,比如使用Maven: 3.编写Dockerfile文件,把步骤2产生 ...

  2. windows使用nc命令基础下载安装---小白篇

    windows使用nc命令 文章源起: 在使用该标题关键词搜索文章,内容多为搬运,且历史悠久. 且,对-l -p 参数未讲解,对小白不友好. 对配置环境变量的方式不理解,误导小白. 对文件解压内容未讲 ...

  3. P3742题解

    思路 只需要让z串做到和y串一样,就得让y串每个字母(题意如此)均小于x串. 所以只要x串有一项小于y串,那么就输出-1,否则输出y串. 下面是核心代码: #include<bits/stdc+ ...

  4. AI绘画:Stable Diffusion 终极炼丹宝典:从入门到精通

    本文收集于教程合集:AIGC从入门到精通教程汇总 我是小梦,以浅显易懂的方式,与大家分享那些实实在在可行之宝藏. 历经耗时数十个小时,总算将这份Stable Diffusion的使用教程整理妥当. 从 ...

  5. 如何通过API接口获取微店的商品详情

    微店是一款电商平台,对于商家而言,了解商品详情数据是非常重要的.通过API接口获取微店的商品详情,可以让商家更加便捷地管理和分析商品数据.下面就让我们详细了解一下如何通过API获取微店的商品详情. 第 ...

  6. 运行解压版tomcat中的startup.bat一闪而退的解决办法

    Tomcat的startup.bat,它调用了catalina.bat,而catalina.bat则调用了setclasspath.bat,只要在setclasspath.bat的开头声明环境变量(红 ...

  7. 16. 从零开始编写一个类nginx工具, 反向代理upstream源码实现

    wmproxy wmproxy将用Rust实现http/https代理, socks5代理, 反向代理, 静态文件服务器,后续将实现websocket代理, 内外网穿透等, 会将实现过程分享出来, 感 ...

  8. calico网络异常,不健康

    解决calico/node is not ready: BIRD is not ready: BGP not established withxxx calico有一个没有ready,查了一下是没有发 ...

  9. 如何将word格式的文档转换成markdown格式的文档

    如何将word格式的文档转换成markdown格式的文档 如何将word格式的文档转换成markdown格式的文档 前言 A. 介绍Markdown和Word格式文档 什么是Markdown? Mar ...

  10. mysql 表级锁之一lock table

    1.lock table t1 read: 1.1.当前线程: 读/写当前表/其他表: unlock tables; lock table t1 read; select * from t1; INS ...