当前位置:网站首页>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;
}
边栏推荐
- What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
- 1007 maximum subsequence sum (25 points) (PAT class a)
- Niuke Xiaobai month race 7 e applese's super ability
- 牛客小白月赛7 E Applese的超能力
- [graduation season] green ant new fermented grains wine, red mud small stove. If it snows late, can you drink a cup?
- 1006 Sign In and Sign Out(25 分)(PAT甲级)
- Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
- Hough transform Hough transform principle
- Educational Codeforces Round 22 E. Army Creation
- abc229 总结(区间最长连续字符 图的联通分量计数)
猜你喜欢

Pointnet / pointnet++ point cloud data set processing and training

Several methods of online database migration

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

Stream stream

Key rendering paths for performance optimization

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

欧拉函数

English语法_名词 - 使用

实战模拟│JWT 登录认证

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,
随机推荐
Jetpack Compose 教程
Master the use of auto analyze in data warehouse
Online data migration scheme encountered in the project 1 - general idea sorting and technical sorting
TCP两次挥手,你见过吗?那四次握手呢?
线上数据库迁移的几种方法
Pytoch learning (4)
How is the entered query SQL statement executed?
Allure of pytest visual test report
Swagger suddenly went crazy
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
HDU 1097 A hard puzzle
水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
数据集划分
2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
Niuke Xiaobai month race 7 who is the divine Archer
Thinking on demand development
Some thoughts on whether the judgment point is located in the contour
socket编程demo二