当前位置:网站首页>3555. 二叉树
3555. 二叉树
2022-08-05 06:42:00 【NEFU AB-IN】
Powered by:NEFU AB-IN
3555. 二叉树
题意
给定一个 n 个结点(编号 1∼n)构成的二叉树,其根结点为 1 号点。
进行 m 次询问,每次询问两个结点之间的最短路径长度。
树中所有边长均为 1。思路
总体思路就是,求出所有结点到根节点的距离,再求出两个点的lca,用距离之和减去lca的距离即可

代码
有注释版
/* * @Author: NEFU AB-IN * @Date: 2022-08-04 09:00:27 * @FilePath: \Acwing\3555\3555.cpp * @LastEditTime: 2022-08-04 09:29:12 */ #include <bits/stdc++.h> using namespace std; #define int long long #define SZ(X) ((int)(X).size()) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define DEBUG(X) cout << #X << ": " << X << '\n' typedef pair<int, int> PII; const int INF = INT_MAX; const int N = 1e6 + 10; void solve() { int n, m; cin >> n >> m; vector<int> g[n + 1], depth(n + 1), st(n + 1); vector<vector<int>> fa(n + 1, vector<int>(11)); // 因为n只有1000,所以指数开到10就够用 for (int i = 1; i <= n; ++i) { int ls, rs; cin >> ls >> rs; if (ls != -1) g[i].push_back(ls); if (rs != -1) g[i].push_back(rs); } function<void(int)> bfs = [&](int root) { queue<int> q; q.push(root); //初始化depth depth[0] = 0; // 哨兵,用来防止越界,代表0的深度为0 depth[root] = 1; st[root] = 1; while (SZ(q)) { auto t = q.front(); q.pop(); for (auto v : g[t]) { if (!st[v]) { st[v] = 1; depth[v] = depth[t] + 1; fa[v][0] = t; // fa数组初始化,v向上跳2的0次方,就是父节点,也就是t for (int k = 1; k <= 10; ++k) // v向上跳2的k次方,也就是先跳2的k-1次方,再跳2的k-1次方 { fa[v][k] = fa[fa[v][k - 1]][k - 1]; } q.push(v); } } } }; function<int(int, int)> lca = [&](int a, int b) { if (depth[a] < depth[b]) // 让a成为要跳的那个 swap(a, b); for (int k = 10; k >= 0; --k) // 开始往上跳,跳到和b同一层 if (depth[fa[a][k]] >= depth[b]) a = fa[a][k]; if (a == b) // 如果这时候相等了,说明b就是lca return a; // 否则一起往上跳,跳到他们的lca的下一层 for (int k = 10; k >= 0; --k) { if (fa[a][k] != fa[b][k]) { a = fa[a][k]; b = fa[b][k]; } } // 返回父节点即可 return fa[a][0]; }; bfs(1); for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; cout << depth[x] + depth[y] - 2 * depth[lca(x, y)] << '\n'; } return; } signed main() { IOS; int T; cin >> T; while (T--) { solve(); } return 0; }无注释版
/* * @Author: NEFU AB-IN * @Date: 2022-08-04 09:00:27 * @FilePath: \Acwing\3555\3555.cpp * @LastEditTime: 2022-08-04 09:29:12 */ #include <bits/stdc++.h> using namespace std; #define int long long #define SZ(X) ((int)(X).size()) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define DEBUG(X) cout << #X << ": " << X << '\n' typedef pair<int, int> PII; const int INF = INT_MAX; const int N = 1e6 + 10; void solve() { int n, m; cin >> n >> m; vector<int> g[n + 1], depth(n + 1), st(n + 1); vector<vector<int>> fa(n + 1, vector<int>(11)); for (int i = 1; i <= n; ++i) { int ls, rs; cin >> ls >> rs; if (ls != -1) g[i].push_back(ls); if (rs != -1) g[i].push_back(rs); } function<void(int)> bfs = [&](int root) { queue<int> q; q.push(root); //初始化depth depth[0] = 0; // 哨兵 depth[root] = 1; st[root] = 1; while (SZ(q)) { auto t = q.front(); q.pop(); for (auto v : g[t]) { if (!st[v]) { st[v] = 1; depth[v] = depth[t] + 1; fa[v][0] = t; for (int k = 1; k <= 10; ++k) { fa[v][k] = fa[fa[v][k - 1]][k - 1]; } q.push(v); } } } }; function<int(int, int)> lca = [&](int a, int b) { if (depth[a] < depth[b]) swap(a, b); for (int k = 10; k >= 0; --k) if (depth[fa[a][k]] >= depth[b]) a = fa[a][k]; if (a == b) return a; for (int k = 10; k >= 0; --k) { if (fa[a][k] != fa[b][k]) { a = fa[a][k]; b = fa[b][k]; } } return fa[a][0]; }; bfs(1); for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; cout << depth[x] + depth[y] - 2 * depth[lca(x, y)] << '\n'; } return; } signed main() { IOS; int T; cin >> T; while (T--) { solve(); } return 0; }
边栏推荐
- [instancetype type Objective-C]
- Kioxia and Aerospike Collaborate to Improve Database Application Performance
- typescript62-泛型工具类型(record)
- Nacos cluster construction
- AI+视频技术助力保障校园安全,校园智能安防平台该如何建设?
- 【C语言】结构体变量数据通过 void* 传入到函数中
- 给网站套上Cloudflare(以腾讯云为例)
- How to avoid online memory leaks
- 【2022 DSCTF决赛wp】
- Shiny04---DT和进度条在shiny中的应用
猜你喜欢

在STM32中使用printf函数

二叉搜索树问题

Nacos cluster construction

Day9 of Hegong Daqiong team vision team training - camera calibration

Put Cloudflare on the website (take Tencent Cloud as an example)

protobuf根据有关联的.proto文件进行编译

TCP sticky packet unpacking problem + solution

re正则表达式

2022杭电多校六 1006-Maex (树形DP)

HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
随机推荐
【动态类型检测 Objective-C】
MySQL: basic part
#Sealos#使用工具部署kubernetesV1.24.0
《PyTorch深度学习实践》第十课(卷积神经网络CNN)
MAYA船的建模
数据库多表关联插入数据
C# FileSystemWatcher
Nacos cluster construction
DNSlog外带数据注入
(2022杭电多校六)1010-Planar graph(最小生成树)
日本卫生设备行业协会:日本温水喷淋马桶座出货量达1亿套
Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
基于快速行进平方法的水面无人船路径规划
IO process thread -> communication between processes -> day7
对数据类型而言运算符无效。运算符为 add,类型为 text。
女生做软件测试会不会成为一个趋势?
re正则表达式
小程序input框不允许输入负数
开启防火墙iptable规则后,系统网络变慢
How to avoid online memory leaks