当前位置:网站首页>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; }
边栏推荐
- Put Cloudflare on the website (take Tencent Cloud as an example)
- Flink学习10:使用idea编写WordCount,并打包运行
- MySQL表操作练习
- #Sealos#使用工具部署kubernetesV1.24.0
- Using printf function in STM32
- 一天学会从抓包到接口测试,通过智慧物业项目深度解析
- IO进程线程->进程间的通信->day7
- Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
- Rapid Medical超小体积且唯一可调的取栓器获得FDA核准
- typescript66-分析partial的实现
猜你喜欢

Shiny04---DT和进度条在shiny中的应用

AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?

MySQL: basic part

typescript62-泛型工具类型(record)

Database table insert data

typescript61-泛型工具类型(pick)

(4) Rotating object detection data roLabelImg to DOTA format

2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams

typescript66-分析partial的实现

binary search tree problem
随机推荐
【动态类型检测 Objective-C】
矩阵的构造
AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
RNote108---显示R程序的运行进度
Flink学习12:DataStreaming API
【JVM调优】Xms和Xmx为什么要保持一致
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
【LeetCode】235.二叉搜索树的最近公共祖先
(四)旋转物体检测数据roLabelImg转DOTA格式
自媒体人一般会从哪里找素材呢?
Technical Analysis Mode (8) Double Top and Bottom
Tencent Internship Summary
Mysql为什么 建立数据库失败
【Dynamic type detection Objective-C】
2022起重机司机(限桥式起重机)考试题库及模拟考试
LabVIEW中如何实现任意形状的不规则按键
Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
typescript60-泛型工具类型(readonly)
MAYA船的建模
Hong Kong International Jewellery Show and Hong Kong International Diamond, Gem and Pearl Show kick off