当前位置:网站首页>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 hidden Markov model and code implementation
- 记一次 .NET 某工控数据采集平台 线程数 爆高分析
- @Data source connection pool exhaustion caused by transactional abuse
- Functional interface
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
- Jetpack Compose 教程
- Basic use of kotlin
- 需求开发思考
- 黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,
- 1006 sign in and sign out (25 points) (PAT class a)
猜你喜欢
Key rendering paths for performance optimization
On communication bus arbitration mechanism and network flow control from the perspective of real-time application
黑马程序员-软件测试--09阶段2-linux和数据库-31-43修改文件权限字母发的说明,-查找链接修改文件,查找文件命令,链接文件,压缩解压方式,vi编辑器基本使用,
Siemens HMI download prompts lack of panel image solution
Pointnext: review pointnet through improved model training and scaling strategies++
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
解密函数计算异步任务能力之「任务的状态及生命周期管理」
YOLOv5s-ShuffleNetV2
What are the consequences of closing the read / write channel?
多表操作-内连接查询
随机推荐
多表操作-内连接查询
1008 Elevator(20 分)(PAT甲级)
Cbcgpprogressdlg progress bar used by BCG
Euler function
What are the consequences of closing the read / write channel?
@transactional滥用导致数据源连接池耗尽问题
Actual combat simulation │ JWT login authentication
2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
C# 使用StopWatch测量程序运行时间
Basic use of kotlin
1008 elevator (20 points) (PAT class a)
明明的随机数
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
Some thoughts on whether the judgment point is located in the contour
Swagger突然发癫
HMM hidden Markov model and code implementation
Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
1009 product of polynomials (25 points) (PAT class a)
Pointnext: review pointnet through improved model training and scaling strategies++
1002. A+b for Polynomials (25) (PAT class a)