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. 快速理解DDD领域驱动设计架构思想-基础篇

    1 前言 本文与大家一起学习并介绍领域驱动设计(Domain Drive Design) 简称DDD,以及为什么我们需要领域驱动设计,它有哪些优缺点,尽量用一些通俗易懂文字来描述讲解领域驱动设计,本篇 ...

  2. IDEA 配置桌面快捷方式

    IDEA 配置桌面快捷方式 目录 IDEA 配置桌面快捷方式 1.下载idea.tar解压 2.配置快捷方式 3.为什么要存放在这个目录? 1.下载idea.tar解压 tar xf ideaIC-2 ...

  3. 小札 Combinatorics & Inclusion-Exclusion Principle 1

    「codeforces - 340E」Iahub and Permutations link. 把 \(1,\dots,n\) 中剩下没被固定的数的数量记作 \(s\),再把这其中不担心有会填到自己身 ...

  4. PostgreSQL学习笔记-3.基础知识:CROSS、INNER、LEFT OUTER、RIGHT OUTER、FULL OUTER、UNION

    PostgreSQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段. 在 PostgreSQL 中,JOIN 有五种连接类型: CROSS JOIN :交叉连接INNER ...

  5. 关于tiptop gp5.2采购模块,价格变更的随笔

    采购价格变更要看具体环节,你可以把他当作是三张表,采购价格表.收货价格表.入库价格表,这些还好处理,如果已抛砖到财务端生成账款再要求改价格就更复杂,会产生更多张表了,改起来也就更复杂. 用apmt91 ...

  6. Windows11如何设置经典的右键菜单

    使用Windows11几个月了,解决了我的电脑经常性彻底死机.蓝屏的问题,系统也流畅.易用了好多.唯一不能忍受的是右键菜单,经常需要再点一次才能找到自己想要的选项,今天网搜了下解决办法,特记录于此. ...

  7. 一次考试的简单T3

    我的第一个想法其实是毫无头绪 根本就想不到dp,直接就写了爆搜后来讲了才知道... 这种dp的状态好像是一类dp的模型,他们的状态都有这样的一维:以第i个数结尾.这样的dp有什么样的标志呢?以第i个数 ...

  8. 【Unity3D】Shader Graph节点

    1 前言 ​ Shader Graph 16.0.3 中有 208 个 Node(节点),本文梳理了 Shader Graph 中大部分 Node 的释义,官方介绍详见→Node-Library. ​ ...

  9. 删除小程序scroll-view的滚动条

    小程序scroll-view滚动条很丑,想隐藏? 在有scroll-view滚动条页面的wxss里添加: ::-webkit-scrollbar { display: none; width: 0; ...

  10. docker入门加实战—部署Java和前端项目

    docker入门加实战-部署Java和前端项目 部署之前,先删除nginx,和自己创建的dd两个容器: docker rm -f nginx dd 部署Java项目 作为演示,我们的Java项目比较简 ...