当前位置:网站首页>Informatics Olympiad 1336: [example 3-1] find roots and children
Informatics Olympiad 1336: [example 3-1] find roots and children
2022-07-04 20:06:00 【Jun Yi_ noip】
【 Topic link 】
ybt 1336:【 example 3-1】 Find roots and children
【 Topic test site 】
1. Trees Parenting
set up pa Array ,pa[i]
It means the first one i The number of the parent node of the node .
【 Their thinking 】
solution 1: Parenting
Save the information of the whole tree with parental representation ,pa[i]
It means the first one i The number of the parent node of the node . Traverse pa Array to find the degree of each node ( The degree of a node in a tree is the number of children in that node ), Get the number of the node with the largest degree . Then traverse pa Array , Output the children of this node .
【 Solution code 】
solution 1: Parenting
#include <bits/stdc++.h>
using namespace std;
int par[101], deg[101];//par[i]: Represents a node i My parents , deg[i]: Represents a node i Degree
int main()
{
int n, m, x, y, root, maxDeg = 0, maxNum = 1;//root: Indicates the root node number ,maxNum: The node number with the highest degree ,maxDeg: Represents the degree of the node with the highest degree , That is, the number of children at the node with many children
cin >> n >> m;
for(int i = 1; i <= m; ++i)
{
cin >> x >> y;
par[y] = x;//y My parents are x
}
for(int i = 1; i <= n; ++i)
{
if(par[i] == 0)// Parents are 0 The node of is the root node
root = i;
deg[par[i]]++;// node i Duega of parents 1
}
cout << root << endl;
for(int i = 1; i <= n; ++i)// Find the node with the greatest degree
{
if(deg[i] > maxDeg)
{
maxDeg = deg[i];// Maximum degree
maxNum = i;// The number of the vertex with the largest degree
}
}
cout << maxNum << endl;
for(int i = 1; i <= n; ++i)// Traverse each node in sequence , See which nodes are the children of the largest nodes
{
if(par[i] == maxNum)
cout << i << ' ';
}
return 0;
}
边栏推荐
- Neural network IOT platform construction (IOT platform construction practical tutorial)
- kotlin 类和对象
- Prometheus installation
- Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
- abc229 总结(区间最长连续字符 图的联通分量计数)
- Niuke Xiaobai month race 7 F question
- Multi table operation - external connection query
- 实战模拟│JWT 登录认证
- 92. (cesium chapter) cesium building layering
- Find the nth power of 2
猜你喜欢
多表操作-内连接查询
Multi table operation inner join query
Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
ACM组合计数入门
92. (cesium chapter) cesium building layering
New wizard effect used by BCG
C语言-入门-基础-语法-流程控制(七)
HMM隐马尔可夫模型最详细讲解与代码实现
随机推荐
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
西门子HMI下载时提示缺少面板映像解决方案
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
HDU 6440 2018 Chinese college student program design network competition
黑马程序员-软件测试--09阶段2-linux和数据库-31-43修改文件权限字母发的说明,-查找链接修改文件,查找文件命令,链接文件,压缩解压方式,vi编辑器基本使用,
Kotlin condition control
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
JVM系列之对象的创建
HDU 1097 A hard puzzle
Pythagorean number law (any three numbers can meet the conditions of Pythagorean theorem)
C language - Introduction - Foundation - grammar - process control (VII)
kotlin 类和对象
Niuke Xiaobai month race 7 who is the divine Archer
【毕业季】绿蚁新醅酒,红泥小火炉。晚来天欲雪,能饮一杯无?
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
矩阵翻转(数组模拟)
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
92. (cesium chapter) cesium building layering
Chrome development tool: what the hell is vmxxx file