当前位置:网站首页>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;
}
边栏推荐
- HMM隐马尔可夫模型最详细讲解与代码实现
- 实战模拟│JWT 登录认证
- BCG 使用之CBCGPProgressDlgCtrl進度條使用
- abc229 总结(区间最长连续字符 图的联通分量计数)
- Euler function
- Basic use of kotlin
- Multi table operation - external connection query
- Introduction to ACM combination counting
- Allure of pytest visual test report
- Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
猜你喜欢
Some thoughts on whether the judgment point is located in the contour
多表操作-外连接查询
黑马程序员-软件测试--08阶段2-linux和数据库-23-30-进程端口相关,修改文件权限,端口号信息的获取,程序和进程相关操作,linux命令案例
Swagger突然发癫
New wizard effect used by BCG
Actual combat simulation │ JWT login authentication
欧拉函数
YOLOv5s-ShuffleNetV2
Hough transform Hough transform principle
Chrome development tool: what the hell is vmxxx file
随机推荐
黑马程序员-软件测试--08阶段2-linux和数据库-23-30-进程端口相关,修改文件权限,端口号信息的获取,程序和进程相关操作,linux命令案例
[graduation season] green ant new fermented grains wine, red mud small stove. If it snows late, can you drink a cup?
Introduction to ACM combination counting
Abc229 summary (connected component count of the longest continuous character graph in the interval)
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
1011 World Cup Betting (20 分)(PAT甲级)
欧拉函数
The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize
SSRS筛选器的IN运算(即包含于)用法
Jetpack compose tutorial
[QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,
Cbcgpprogressdlg progress bar used by BCG
Neural network IOT platform construction (IOT platform construction practical tutorial)
牛客小白月赛7 谁是神箭手
需求开发思考
Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,
2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
1009 Product of Polynomials(25 分)(PAT甲级)
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company