当前位置:网站首页>Binary tree (Beijing University of Posts and Telecommunications machine test questions) (day85)
Binary tree (Beijing University of Posts and Telecommunications machine test questions) (day85)
2022-07-27 03:40:00 【Zhangxueheng】
List of articles
1: subject
Given a n Nodes ( Number 1∼n) A binary tree , Its root node is 1 Number point .
Conduct m Time to ask , Ask the shortest path length between two nodes each time .
All sides of the tree are 1.
Input format
The first line contains an integer T, Expressing common ownership T Group test data .
The first row of each set of data contains two integers n,m.
Next n That's ok , Each line contains two integers , Among them the first i The integer of the row represents the node i Child node number of . If there is no child node, output −1.
Next m That's ok , Each line contains two integers , Indicates the number of the two nodes to be queried .
Output format
Output of each group of test data m That's ok , Represents the shortest path length between two nodes of the query .
Data range
1≤T≤10,
1≤n,m≤1000
sample input :
1
8 4
2 3
4 5
6 -1
-1 -1
-1 7
-1 -1
8 -1
-1 -1
1 6
4 6
4 5
8 1
sample output :
2
4
2
4
difficulty : Simple
when / Empty limit :1s / 64MB
Total number of passes :654
Total attempts :1453
source : Beijing University of Posts and Telecommunications postgraduate entrance examination questions
Algorithm tags
2: Code implementation
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
int l[N], r[N], p[N];
int dist[N];
void dfs(int u, int d)
{
dist[u] = d;
if (l[u] != -1) dfs(l[u], d + 1);
if (r[u] != -1) dfs(r[u], d + 1);
}
int get_lca(int a, int b)
{
if (dist[a] < dist[b]) return get_lca(b, a);
while (dist[a] > dist[b]) a = p[a];
while (a != b) a = p[a], b = p[b];
return a;
}
int main()
{
int T;
cin >> T;
while (T -- )
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
{
int a, b;
cin >> a >> b;
l[i] = a, r[i] = b;
if (a != -1) p[a] = i;
if (b != -1) p[b] = i;
}
dfs(1, 0);
while (m -- )
{
int a, b;
cin >> a >> b;
int c = get_lca(a, b);
cout << dist[a] + dist[b] - dist[c] * 2 << endl;
}
}
return 0;
}
边栏推荐
- Introduction to database - a brief introduction to MySQL
- [understanding of opportunity -52]: the depth of communication varies from person to person
- Win10/win11 lossless expansion of C disk space, cross disk consolidation of C and e disks
- Double disk: the main differences between DFS and BFS, the differences in ideology, and the differences in code implementation
- Customer cases | pay attention to the elderly user experience, and the transformation of bank app to adapt to aging should avoid falsehood and be practical
- Deployment of ruoyi's environment and operation of the system
- Byte side: can TCP and UDP use the same port?
- Food chain (day 79)
- How many implementation postures of delay queue? Daily essential skills!
- MySQL has a nonexistent error
猜你喜欢

flask_ Reqparse parser inheritance in restful

Code practice when the queue reaches the maximum length

spark学习笔记(五)——sparkcore核心编程-RDD转换算子

MySQL underlying data structure

MySQL has a nonexistent error

基于OpenCV的轮廓检测(1)
![[untitled] JDBC connection database read timeout](/img/24/726ed8b3419866244a1b69e6485d7c.png)
[untitled] JDBC connection database read timeout

MySQL的数据库有关操作

app端接口用例设计方法和测试方法

Contour detection based on OpenCV (1)
随机推荐
Redis源码学习(33),命令执行过程
Double disk: the main differences between DFS and BFS, the differences in ideology, and the differences in code implementation
Sqlserver select * can you exclude a field
Wechat applet generation Excel
FastBoot brush machine
Customer cases | pay attention to the elderly user experience, and the transformation of bank app to adapt to aging should avoid falsehood and be practical
Explain tool actual operation
[common search questions] 111
百融榕树数据分析拆解方法
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
Number of 0 at the end of factorial
768. Block II greed that can complete sorting at most
Spark Learning Notes (VI) -- spark core core programming RDD action operator
How to interact with the server when the client sends an SQL message
JMeter distributed pressure measurement
Explain工具实际操作
The diagram of user login verification process is well written!
LPCI-252通用型PCI接口CAN卡的功能和应用介绍
Deployment of ruoyi's environment and operation of the system
If the detailed explanation is generated according to the frame code