当前位置:网站首页>谁的孙子最多II
谁的孙子最多II
2022-08-03 19:46:00 【-JMY-】
题目描述
给定一棵树,其中1号结点是根结点,问哪一个结点的孙子结点最多,有多少个。(孙子结点,就是儿子结点的儿子结点。)
输入
第一行一个整数N(N≤10000),表示树结点的个数。此后N-1行,第i行包含一个整数Fi,表示i+1号结点的父亲。
输出
一行两个整数,表示孙子结点最多的结点,以及其孙子结点的个数,如果有多个,输出编号最小的。
样例输入
5 1 1 2 4
样例输出
1 1
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n,x,a[105],t[105],maxn,imaxn;
int main(){
cin>>n;
for(int i=1;i<n;i++){
cin>>x;
a[i+1]=x;
}
for(int i=1;i<=n;i++)
if(a[a[i]]!=0&&a[i]!=0)
t[a[a[i]]]++;
for(int i=1;i<=n;i++){
if(t[i]>maxn){
maxn=t[i];
imaxn=i;
}
}
cout<<imaxn<<' '<<maxn;
return 0;
}
边栏推荐
- Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
- The effective square of the test (one question of the day 7/29)
- relocation R_X86_64_PC32 against,/usr/bin/ld: final link failed: Bad value
- DeepMCP网络详解
- 【统计机器学习】线性回归模型
- Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
- Node version switching tool NVM and npm source manager nrm
- 宁德时代2号人物黄世霖辞任副董事长:身价1370亿
- List类的超详细解析!(超2w+字)
- 开源教育论坛| ChinaOSC
猜你喜欢

钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?

Shell programming loop statement

Word另存为PDF后无导航栏解决办法

List类的超详细解析!(超2w+字)
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting

基于移动GIS的环保生态管理系统

Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法

简易电子琴设计(c语言)

Anaconda 虚拟环境迁移

LeetCode 622. Designing Circular Queues
随机推荐
pg_memory_barrier_impl in Postgresql and C's volatile
涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流
余弦距离介绍
MVC vs MVP
LeetCode 622. 设计循环队列
高效目标检测:动态候选较大程度提升检测精度(附论文下载)
Brush the topic of mobile zero power button
Power button brush the topic of merging two orderly array
【统计机器学习】线性回归模型
The addition and subtraction of the score of the force deduction brush question (a daily question 7/27)
matplotlib画polygon, circle
The effective square of the test (one question of the day 7/29)
Use ControlTemplate or Style from resource file in WPF .cs and find the control
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
redis常用命令,HSET,XADD,XREAD,DEL等
高并发,你真的理解透彻了吗?
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
net-snmp私有mib动态加载到snmpd
In-depth understanding of JVM-memory structure
ECCV2022 | 用于视频问题回答的视频图Transformer