[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 ...
随机推荐
- 【路由器】OpenWrt 手动编译 ipk
目录 .ipk 文件 编译准备 编译 .ipk 文件 更新 feeds 配置平台 获取交叉编译链 添加需要编译的第三方软件包 参考资料 .ipk 文件 .ipk 文件是可以通过 OpenWrt 的包管 ...
- ImGui界面优化:使用图标字体、隐藏主窗口标题栏
目录 使用图标字体 扩展:内存加载字体 隐藏主窗口标题栏 增加程序退出 改进HideTabBar 窗口最大化 总结 本文主要介绍ImGui应用中的一些界面优化方法,如果是第一次使用ImGui推荐从上一 ...
- Web通用漏洞--sql注入
SQL注入 mysql注入目的:获取当前web权限 mysql注入--常规查询&union联合查询 MYSQL--Web组成架构 服务器搭建web服务可能存在多个站点搭建在一台服务器中,数据集 ...
- 用 ChatGPT 做一个 Chrome 扩展 | 京东云技术团队
用ChatGPT做了个Chrome Extension 最近科技圈儿最火的话题莫过于ChatGPT了. 最近又发布了GPT-4,发布会上的Demo着实吸睛. 笔记本上手画个网页原型,直接生成网页.网友 ...
- Solution Set -「CSP-S 2020」
Problem. 1 - Junior Julian 模拟模拟模拟摸死 CTR 的母. 考场代码: #include<cstdio> namespace solveIt { void re ...
- vue指令 v-if
1.字符'0'也显示为真 <div v-if="zeroStr">明月几时有,把酒问青天.</div> data() { zeroStr: '0' } 运行 ...
- 聊聊基于Alink库的决策树模型算法实现
示例代码及相关内容来源于<Alink权威指南(Java版)> 概述 决策树模型再现了人们做决策的过程,该过程由一系列的判断构成,后面的判断基于前面的判断结果,不断缩小范围,最终推出结果. ...
- 聊聊基于Alink库的主成分分析(PCA)
概述 主成分分析(Principal Component Analysis,PCA)是一种常用的数据降维和特征提取技术,用于将高维数据转换为低维的特征空间.其目标是通过线性变换将原始特征转化为一组新的 ...
- Git——Git 常用命令
文章目录 仓库 配置 增加/删除文件 代码提交 分支 标签 查看信息 远程同步 撤销 其他 仓库 # 在当前目录新建一个Git代码库 $ git init # 新建一个目录,将其初始化为Git代码库 ...
- c# 光学三原色混合,颜色叠加-dong
东的备注: 光的三原色:红.绿.蓝 红+绿=黄 红+蓝=品红 蓝+绿=青 红+绿+蓝=白 无颜色为黑 下看代码 Bitmap image1 = new Bitmap(500, 500);//红 Bit ...