当前位置:网站首页>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;
}
边栏推荐
- Application practice | Shuhai supply chain construction of data center based on Apache Doris
- Explicit random number
- New wizard effect used by BCG
- 1007 maximum subsequence sum (25 points) (PAT class a)
- Online data migration scheme encountered in the project 1 - general idea sorting and technical sorting
- 明明的随机数
- Key rendering paths for performance optimization
- abc229 总结(区间最长连续字符 图的联通分量计数)
- ACM组合计数入门
- Allure of pytest visual test report
猜你喜欢
![[problem] Druid reports exception SQL injection violation, part always true condition not allow solution](/img/cc/160bc8ccdc378901510c1b61c3f5d3.png)
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution

Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)

On communication bus arbitration mechanism and network flow control from the perspective of real-time application

西门子HMI下载时提示缺少面板映像解决方案

BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)

What are the consequences of closing the read / write channel?

Pointnext: review pointnet through improved model training and scaling strategies++
Some thoughts on whether the judgment point is located in the contour

Chrome开发工具:VMxxx文件是什么鬼

JVM系列之对象的创建
随机推荐
Data set division
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
kotlin 继承
Educational Codeforces Round 22 E. Army Creation
In operation (i.e. included in) usage of SSRs filter
TCP两次挥手,你见过吗?那四次握手呢?
1011 World Cup betting (20 points) (pat a)
多表操作-内连接查询
Multi table operation - external connection query
项目中遇到的线上数据迁移方案1---总体思路整理和技术梳理
ACM组合计数入门
C语言-入门-基础-语法-流程控制(七)
HDU 6440 2018 Chinese college student program design network competition
Siemens HMI download prompts lack of panel image solution
Hough transform Hough transform principle
Free soldier
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
C language - Introduction - Foundation - grammar - process control (VII)
输入的查询SQL语句,是如何执行的?
BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)