当前位置:网站首页>信息学奥赛一本通 1336:【例3-1】找树根和孩子
信息学奥赛一本通 1336:【例3-1】找树根和孩子
2022-07-04 17:14:00 【君义_noip】
【题目链接】
【题目考点】
1. 树 双亲表示法
设pa数组,pa[i]表示第i结点的双亲结点的编号。
【解题思路】
解法1:双亲表示法
用双亲表示法保存整棵树的信息,pa[i]表示第i结点的双亲结点的编号。遍历pa数组求出每个结点的度(树中结点的度就是该结点孩子的数量),得到度最大的结点的编号。然后遍历pa数组,输出该结点的孩子。
【题解代码】
解法1:双亲表示法
#include <bits/stdc++.h>
using namespace std;
int par[101], deg[101];//par[i]:表示结点i的双亲, deg[i]:表示结点i的度
int main()
{
int n, m, x, y, root, maxDeg = 0, maxNum = 1;//root:表示根结点编号,maxNum:度最高结点编号,maxDeg:表示度最高结点的度,即孩子对多的结点的孩子数
cin >> n >> m;
for(int i = 1; i <= m; ++i)
{
cin >> x >> y;
par[y] = x;//y的双亲是x
}
for(int i = 1; i <= n; ++i)
{
if(par[i] == 0)//双亲是0的结点是根结点
root = i;
deg[par[i]]++;//结点i的双亲的度加1
}
cout << root << endl;
for(int i = 1; i <= n; ++i)//查找度最大的结点
{
if(deg[i] > maxDeg)
{
maxDeg = deg[i];//最大度数
maxNum = i;//度数最大的顶点的编号
}
}
cout << maxNum << endl;
for(int i = 1; i <= n; ++i)//顺序遍历各个结点,看哪些结点是度最大结点的孩子
{
if(par[i] == maxNum)
cout << i << ' ';
}
return 0;
}
边栏推荐
- 机器学习概念漂移检测方法(Aporia)
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
- 输入的查询SQL语句,是如何执行的?
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Mysql5.7 installation tutorial graphic explanation
- [system disk back to U disk] record the operation of system disk back to U disk
- 同事悄悄告诉我,飞书通知还能这样玩
- An example of multi module collaboration based on NCF
- PB的扩展DLL开发(超级篇)(七)
- How to improve development quality
猜你喜欢

Scala basic tutorial -- 17 -- Collection

NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读

MySQL common add, delete, modify and query operations (crud)
![[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors

MXNet对GoogLeNet的实现(并行连结网络)

能源行业的数字化“新”运维

TCP waves twice, have you seen it? What about four handshakes?

ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development

Numpy 的仿制 2

How to improve development quality
随机推荐
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
How to improve development quality
Scala基础教程--19--Actor
Pb extended DLL development (super chapter) (VII)
【210】PHP 定界符的用法
力扣刷题日记/day2/2022.6.24
Scala basic tutorial -- 20 -- akka
Li Kou brush question diary /day8/7.1
How to modify icons in VBS or VBE
Scala基础教程--16--泛型
Lua EmmyLua 注解详解
C language printing exercise
LD_ LIBRARY_ Path environment variable setting
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
力扣刷題日記/day6/6.28
How to download files using WGet and curl
中国农科院基因组所汪鸿儒课题组诚邀加入
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
Caché JSON 使用JSON适配器
Scala基础教程--12--读写数据