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. 14.4K Star,一款外观漂亮、运行快速、动画细腻的开源免费UI组件库

    之前给大家推荐了很多后台模版,有读者希望推荐一些跟通用的好看组件,毕竟出了后台还有很多其他场景嘛.所以,今天继续给大家推荐一个广受好评的UI组件库:NextUI 主要特性 NextUI的主要目标是简化 ...

  3. xv6 中的进程切换:MIT6.s081/6.828 lectrue11:Scheduling 以及 Lab6 Thread 心得

    絮絮叨 这两节主要介绍 xv6 中的线程切换,首先预警说明,这节课程的容量和第 5/6 节:进程的用户态到内核态的切换一样,细节多到爆炸,连我自己复习时都有点懵,看来以后不能偷懒了,学完课程之后要马上 ...

  4. MySQL高级10-InnoDB引擎存储架构

    一.逻辑存储结构 表空间(Tablespace):一个mysql实例,及一个数据库实例,可以对应多个表空间(ibd文件),用于存储记录,索引等数据. 段(Segment):分为数据段(Leaf nod ...

  5. Note -「Suffix Automaton」SAM

    Part. 1 基本信息 Part. 1-1 SAM 的构成. SAM 由两个东西构成,一个是一个 DAWG,还有一棵外向树,叫 parent tree. 比如,给你一个字符串 \(S=\sf abb ...

  6. Python基础—— 模块介绍、模块的使用、包介绍、 包的使用、软件开发目录规范

    文章目录 一 模块介绍 二 模块的使用 2.1 import语句 2.2 from-import 语句 2.3 其他导入语法(as) 2.4 循环导入问题 2.5 搜索模块的路径与优先级 2.6 区分 ...

  7. LVS+keepalived配置高可用架构和负载均衡机制(2)

    一.概述 接上文,实际生产场景中,往往存在硬件资源数量的限制,此时需要设置DS节点复用RS节点. 所以往往最常见的架构如下图所示: 3台主机组建真实服务器集群,即3个RS 2个RS兼做DS,构建负载均 ...

  8. springboot整合redis报错:链接失败; Unable to connect to Redis

    springboot整合redis报错:链接失败:org.springframework.data.redis.RedisConnectionFailureException: Unable to c ...

  9. Java基础知识1-10

    测试要点 一.Java基础 1.常用设计模式有哪些?在项目中有哪里用的到?单例中懒汉饿汉优缺点? 软件设计模式分为三类分别为创建型.结构型.行为型. 1.1创建型 1.1.1单例模式(singleto ...

  10. 【vulnhub】——DC-9靶机

    [vulnhub]--DC-9靶机 1. 主机发现 扫描kali主机C段(Kali和DC-9主机在同一个网关下): 发现主机为192.168.108.146,进行详细端口扫描: 可以看到靶机开了一个s ...