[ABC263B] Ancestor
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的更多相关文章
- [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 ...
- [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 ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- [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 ...
- 数据结构与算法(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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 使用PySpark计算AUC,KS与PSI
当特征数量或者模型数量很多的时候,使用PySpark去计算相关指标会节省很多的时间.网上关于使用PySpark计算相关指标的资料较少,这里抛砖引玉,写了三个风控常用的指标AUC,KS和PSI相关的计算 ...
- ETL之apache hop数据增量同步功能
ETL增量数据抽取CDC 概念:Change Data Capture,变化的数据捕获,也称:[增量数据抽取](名词解释) CDC是一种实现数据的增量抽取解决方案,是实现[ETL整体解决方案]中的一项 ...
- Docker 安装Redis 无法使用配置文件设置密码问题
背景 最近开发需要使用各种组件,如果都到开发机上安装,会占用电脑资源较多.所以使用docker容器来安装这些组件.例如 redis .mongodb.mysql.rabitmq.elasticsear ...
- 手写raft(三) 实现日志压缩
手写raft(三) 实现日志压缩 在上一篇博客中MyRaft实现了日志复制功能,按照计划接下来需要实现日志压缩. 手写raft(一) 实现leader选举 手写raft(二) 实现日志复制 1. 什么 ...
- JS遍历Json串并获取Key和Value
//data为json串 for (var key in data) { console.log(key); console.log(data[key]); }
- 原来你是这样的JAVA[04]-数组Arrays
一.打印数组 Arrays类提供了打印数组元素的方法,Arrays.toString()和Arrays.deepToString(). //打印数组 System.out.println(Arrays ...
- 正则表达式快速入门三: python re module + regex 匹配示例
使用 Python 实现不同的正则匹配(从literal character到 其他常见用例) reference python regular expression tutorial 目录 impo ...
- MyBatis-Plus和PageHelper冲突导致Factory method sqlSessionFactory threw exception,并且如何分页显示全部
springboot开始引入了mybaits-plus.后来想引入pagehelper进行分页,引入之后报错 Error starting ApplicationContext. To display ...
- Record -「Tricks」记录
曼哈顿距离 \(\text{dist}(A,B)=|x_{A}-x_{B}|+|y_{A}-y_{B}|\) 可以拆成 \(\max\{x_{A}-x_{B}+y_{A}-y_{B},x_{A}-x_ ...
- 利用SpringBoot项目做一个Mock挡板;基于事件发布动态自定义URL和响应报文
导入SpringbootWEb依赖 <!--web项目驱动--> <dependency> <groupId>org.springframework.boot< ...