当前位置:网站首页>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; }
边栏推荐
- typescript67-索引查询类型
- Falsely bamboo brother today and found a localization of API to use tools
- 2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams
- (4) Rotating object detection data roLabelImg to DOTA format
- Modeling of the MAYA ship
- Week 8 Document Clustering
- The NDK compiler so libraries
- MySQL表操作练习
- Rapid Medical超小体积且唯一可调的取栓器获得FDA核准
- LaTeX Notes
猜你喜欢
随机推荐
re正则表达式
软件测试必问面试题(附答案和解析)
2022熔化焊接与热切割操作证考试题及模拟考试
MySQL: JDBC programming
【instancetype类型 Objective-C】
合工大苍穹战队视觉组培训Day9——相机标定
It turns out that Maya Arnold can also render high-quality works!Awesome Tips
利用将网页项目部署到阿里云上(ngnix)
文本特征化方法总结
技术分析模式(九)三重顶部和底部
The NDK compiler so libraries
Using printf function in STM32
C# FileSystemWatcher
Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
(2022杭电多校六)1012-Loop(单调栈+思维)
TCP sticky packet unpacking problem + solution
MySQL:JDBC编程
【2022 DSCTF决赛wp】
真实字节跳动测试开发面试题,拿下年薪50万offer。
线程池的使用(结合Future/Callable使用)