当前位置:网站首页>谁的孙子最多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;
}
边栏推荐
- 边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型
- 揭秘5名运维如何轻松管理数亿级流量系统
- 【统计机器学习】线性回归模型
- 高位套牢机构,用友网络的信任危机是如何产生的?
- 小马智行起诉擎天智卡:索赔6000万 彭军称要斗争到底
- Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
- Network protocol-TCP, UDP difference and TCP three-way handshake, four wave
- MySQL Basics
- Shell programming loop statement
- 基于移动GIS的环保生态管理系统
猜你喜欢

Jingdong cloud released a new generation of distributed database StarDB 5.0

The ecological environmental protection management system based on mobile GIS
手把手教你定位线上MySQL慢查询问题,包教包会

「游戏建模干货」建模大师几步操作,学习经典,赶紧脑补一下吧

告诉你0基础怎么学好游戏建模?

盘点在线帮助中心对企业能够起到的作用

1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)

百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东

MySQL基础

花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
随机推荐
MVC vs MVP
Network protocol-TCP, UDP difference and TCP three-way handshake, four wave
Jingdong cloud released a new generation of distributed database StarDB 5.0
盘点在线帮助中心对企业能够起到的作用
ADS 2023 下载链接
The effective square of the test (one question of the day 7/29)
LeetCode 952. Calculate Maximum Component Size by Common Factor
虚拟机vmware设置nat模式上网
揭秘5名运维如何轻松管理数亿级流量系统
Detailed steps for tensorflow-gpu2.4.1 installation and configuration
Postgresql中的pg_memory_barrier_impl和C的volatile
149. 直线上最多的点数-并查集做法
机器学习中专业术语的个人理解与总结(纯小白)
JWT详解
危化企业双重预防机制数字化建设进入全面实施阶段
【STM32】标准库-自定义BootLoader
「学习笔记」高斯消元
Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
Line the last time the JVM FullGC make didn't sleep all night, collapse