当前位置:网站首页>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)
- 1005 Spell It Right(20 分)(PAT甲级)
- Chrome开发工具:VMxxx文件是什么鬼
- What are the consequences of closing the read / write channel?
- 【毕业季】绿蚁新醅酒,红泥小火炉。晚来天欲雪,能饮一杯无?
- 黑马程序员-软件测试--09阶段2-linux和数据库-31-43修改文件权限字母发的说明,-查找链接修改文件,查找文件命令,链接文件,压缩解压方式,vi编辑器基本使用,
- Stream stream
- 实战模拟│JWT 登录认证
- c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
- Euler function
猜你喜欢
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
C语言-入门-基础-语法-流程控制(七)
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
Euler function
SSRS筛选器的IN运算(即包含于)用法
The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize
Swagger突然发癫
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
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,
New wizard effect used by BCG
随机推荐
Add namespace declaration
Development and construction of DFI ecological NFT mobile mining system
Key rendering paths for performance optimization
解密函数计算异步任务能力之「任务的状态及生命周期管理」
BCG 使用之CBCGPProgressDlgCtrl进度条使用
Thinking on demand development
1005 Spell It Right(20 分)(PAT甲级)
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
Introduction to ACM combination counting
Neural network IOT platform construction (IOT platform construction practical tutorial)
Find the nth power of 2
Free soldier
Cbcgpprogressdlgctrl progress bar used by BCG
Niuke Xiaobai month race 7 who is the divine Archer
黑马程序员-软件测试--08阶段2-linux和数据库-23-30-进程端口相关,修改文件权限,端口号信息的获取,程序和进程相关操作,linux命令案例
BCG 使用之CBCGPProgressDlgCtrl進度條使用
1006 sign in and sign out (25 points) (PAT class a)
Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
Pointnext: review pointnet through improved model training and scaling strategies++
1007 maximum subsequence sum (25 points) (PAT class a)