[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 ...
随机推荐
- Python 基础面试第四弹
1. Python中常用的库有哪些,作用分别是什么 requests: requests 是一个用于发送 HTTP 请求的库,它提供了简单而优雅的 API,可以轻松地发送 GET.POST.PUT.D ...
- api接口对接如何实现商品数据采集的
在当前互联网行业中,快速准确地采集和处理大量数据是非常重要的一项任务.而实现商品数据采集则是许多企业和电商平台必须完成的任务之一.使用API接口对接进行商品数据采集可以大大提高数据采集效率和准确性.下 ...
- 前瞻|Java 21 新特性 String Templates(字符串模版)
在日常写Java的时候,对于字符串的操作是非常普遍的,其中最常见的就是对字符串的组织.也因为这个操作非常普遍,所以诞生了很多方案,总下来大概有这么几种: 使用+拼接 使用StringBuffer和Sp ...
- redis单机、主从、哨兵、集群以及redisson分布式锁
1.搭建集群 Linux系统的Redis各版本下载路径:https://download.redis.io/releases/,建议下载5.0以上的版本,下载后进行解压安装 (1)单机版 安装环境 y ...
- 两个例子带你入门 Disruptor
Disruptor 是英国外汇交易公司 LMAX 开发的一个高性能队列.很多知名开源项目里,比如 canal .log4j2. storm 都是用了 Disruptor 以提升系统性能 . 这篇文章, ...
- 理解并掌握C#的Channel:从使用案例到源码解读(一)
引言 在C#的并发编程中,Channel是一种非常强大的数据结构,用于在生产者和消费者之间进行通信.本文将首先通过一个实际的使用案例,介绍如何在C#中使用Channel,然后深入到Channel的源码 ...
- Oracle-降低表的高水位线
在应用中存在一系列的表,对表的操作是批量插入又批量删除,最终导致表的水位线很高.高水位线影响全索引扫描的SQL.即影响系统的性能. 现有方法降低表的水位线: 1.降低表的高水位线 select 'al ...
- 11g编译bbed
报错如下: make -f ins_rdbms.mk $ORACLE_HOME/rdbms/lib/bbed Linking BBED utility (bbed) rm -f /u01/app/or ...
- php反序列化--[SWPUCTF 2021 新生赛]no_wakeup
打开网站发现这个,点击 ??? 就看到了代码: 发现是PHP反序列化, 但和一般的PHP反序列化不同的是,多了一个_wakeup函数,然后就去网上搜了一下, 发现是一个cve漏洞CVE-2016-7 ...
- SOA认知和方法论
1 前言 1.1 架构分类 在软件设计领域,企业架构通常被划分为如下五种分类: 如何理解架构分类依据及其彼此之间的关系?业务是企业赖以生存之本,因此业务架构是基础.是灵魂,其他一切均是对业务架构的支撑 ...