当前位置:网站首页>BUAA magpie nesting
BUAA magpie nesting
2022-07-06 03:30:00 【star-399】
【 Problem description 】
Magpies usually choose a tree with the highest branches ( That is, the node with the highest height among the nodes with the most branches ) Nest in a place ( The structure is the most stable ). Given a trident tree , Calculate the location where magpies build their nests ( There may be more than one , When there are multiple nodes, the corresponding node information should be given in the order of traversing the tree , Including the node number and the number of accesses to the node in the previous order ). For example, the following figure is a trident tree :
The node in the tree has at most three branches , The highest node with three branches is 23 Number and 15 Node No , According to the previous order, the traversal order will be 19 Accessed to 23 Node No , The first 31 Accessed to 15 Node No .
requirement : When traversing the branches of the Trident tree according to the previous order, you should first access the left branch , Then visit the middle branch , Finally, visit the right branch ; The root node is 1 Access nodes , And there are at most 100 Nodes .
【 Input form 】
First, input an integer from the console to represent the number of entries in the tree node relationship , Then start on the next line , Enter the relationship between tree nodes from the root . The node number is from number 1 Start , Such as 1 Represents the root node of the tree , The number of other nodes is greater than 1, No repetition , But the numbering is irregular . The relationship between nodes in the tree is described in the following way :
R S1 S2 S3
among R Is the bifurcation node ,S1,S2,S3 They are tree forks R Left of 、 in 、 Right child node , Because it is a trident tree ,S1,S2,S3 At most 2 Item is empty , Used when this item is empty 0 Express . Each item is separated by a space , Finally, there is a carriage return . Such as :
1 5 6 0
Indicate number 1 The root of the tree has two forks , The numbers are 5 and 6.
Be careful : The input order of node relationship is to input the relationship between root and child nodes from the root node in turn , Then enter the relationship between the bifurcation node and the child node , When entering a node relationship , Its parent node must already exist , But not necessarily according to the level of input .
【 Output form 】
Output the node information with the highest height among the nodes with the most branches according to the previous traversal order , Each node information occupies one line , It includes the node number and the number of accesses to the node in the previous order , Separate data with a space .
【 The sample input 】
18
1 2 3 4
4 5 0 0
3 6 7 8
2 9 10 0
7 21 22 0
21 23 24 0
23 25 26 27
9 28 29 0
28 30 31 37
30 32 0 0
31 33 36 0
33 34 35 0
5 11 12 13
11 14 0 0
12 15 0 0
13 16 0 0
15 17 18 19
19 20 0 0
【 Sample output 】
23 19
15 31
【 Sample explanation 】
Input 18 Tree node relationship , The trigeminal tree established according to these relationships is shown in the above figure . The node in the tree has at most three branches , The highest node with three branches is 23 Number and 15 Node No , According to the previous order, the traversal order will be 19 Accessed to 23 Node No , The first 31 Accessed to 15 Node No . The root node is 1 Visited nodes .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 102
struct triple_tree
{
int num;
struct triple_tree *child[3];
};
typedef struct queue
{
int num, when, level, point;
} queue;
typedef struct triple_tree tree;
queue result[MAXSIZE];
tree nodes[MAXSIZE];
int n, i, cup[3], head, j;
int count;
void tree_vt(tree *root, int level)
{
result[count].level = level;
result[count].num = root->num;
result[count].when = count + 1;
result[count].point = 0;
for (i = 0; i < 3; i++)
{
if (root->child[i] != NULL)
{
result[count].point += 1;
}
}
count++;
return;
}
void fvisit(tree *root, int level)
{
int i;
tree_vt(root, level);
for (i = 0; i < 3; i++)
{
if (root->child[i] != NULL)
{
fvisit(root->child[i], level + 1);
}
}
return;
}
int cmp(queue *a, queue *b)
{
if (a->point > b->point)
return -1;
else if (a->point == b->point && a->level > b->level)
return -1;
else if (a->point == b->point && a->level == b->level && a->when < b->when)
return -1;
return 1;
}
int main()
{
scanf("%d", &n);
while (n--)
{
scanf("%d%d%d%d", &head, &cup[0], &cup[1], &cup[2]);
for (i = 0; i < 3; i++)
{
if (cup[i] != 0)
nodes[head - 1].child[i] = &nodes[cup[i] - 1];
else
nodes[head - 1].child[i] = NULL;
}
}
for (i = 0; i < MAXSIZE; i++)
nodes[i].num = i + 1;
fvisit(&nodes[0], 1);
qsort(&result[0], count + 1, sizeof(queue), (const void *)cmp);
i = 0;
n = result[0].level;
while (result[i].level == n)
{
printf("%d %d\n", result[i].num, result[i].when);
i++;
}
return 0;
}
BUAAer?
边栏推荐
- 施努卡:视觉定位系统 视觉定位系统的工作原理
- BUAA喜鹊筑巢
- 遥感图像超分辨率论文推荐
- Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
- EDCircles: A real-time circle detector with a false detection control 翻译
- Recommended papers on remote sensing image super-resolution
- Tidb ecological tools (backup, migration, import / export) collation
- 暑期刷题-Day3
- 2.2 STM32 GPIO操作
- Performance analysis of user login TPS low and CPU full
猜你喜欢
NR modulation 1
Svg drag point crop image JS effect
2、GPIO相关操作
BUAA计算器(表达式计算-表达式树实现)
IPv6 comprehensive experiment
SAP ALV color code corresponding color (finishing)
给新人工程师组员的建议
js凡客banner轮播图js特效
Canvas cut blocks game code
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
随机推荐
Schnuka: visual positioning system working principle of visual positioning system
Recommended foreign websites for programmers to learn
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
Distributed service framework dobbo
Advanced learning of MySQL -- Fundamentals -- isolation level of transactions
Map sorts according to the key value (ascending plus descending)
1. New project
Overview of super-resolution reconstruction of remote sensing images
These are not very good
Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
1.16 - check code
mysqldump数据备份
Esbuild & SWC: a new generation of construction tools
Cross origin cross domain request
SAP ALV color code corresponding color (finishing)
3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
【概念】Web 基础概念认知
ESBuild & SWC浅谈: 新一代构建工具
The solution of permission denied (750 permissions should be used with caution)
Restful style