Skip to content

Commit c0c61b3

Browse files
hitonanodeweb-flow
andauthored
LCA(RMQ) Add Document (#63)
* LCA(RMQ) Add Document * [auto-verifier] verify commit 6c981ef Co-authored-by: GitHub <noreply@github.com>
1 parent 50b95ad commit c0c61b3

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@
150150
"tree/test/diameter.test.cpp": "2021-06-09 00:31:07 +0900",
151151
"tree/test/frequency_table_of_tree_distance.test.cpp": "2021-06-06 15:23:40 +0900",
152152
"tree/test/hl_decomposition.test.cpp": "2021-06-06 15:23:40 +0900",
153-
"tree/test/lca.test.cpp": "2021-07-17 20:15:48 +0900",
154-
"tree/test/lca.yuki898.test.cpp": "2021-07-17 20:15:48 +0900",
155-
"tree/test/lca_rmq.test.cpp": "2021-05-01 20:55:29 +0900",
153+
"tree/test/lca.test.cpp": "2021-07-30 23:25:20 +0900",
154+
"tree/test/lca.yuki898.test.cpp": "2021-07-30 23:25:20 +0900",
155+
"tree/test/lca_rmq.test.cpp": "2021-07-30 23:25:20 +0900",
156156
"tree/test/tree_isomorphism.aoj1613.test.cpp": "2021-07-30 01:44:42 +0900",
157157
"tree/test/vertex-add-path-sum.test.cpp": "2021-06-06 15:23:40 +0900",
158158
"tree/test/vertex-add-subtree-sum.test.cpp": "2021-06-06 15:23:40 +0900",

tree/lca_rmq.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ struct TreeLCA {
5656
if (a > b) std::swap(a, b);
5757
return rmq.get(a, b + 1).second;
5858
};
59+
60+
int path_length(int u, int v) { return depth[u] + depth[v] - depth[lca(u, v)] * 2; }
5961
};

tree/lca_rmq.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Lowest common ancestor of tree based on sparse table (クエリ $O(1)$ の最小共通祖先)
3+
documentation_of: ./lca_rmq.hpp
4+
---
5+
6+
根を固定した木に対する 2 頂点の最小共通祖先,および 2 頂点間の距離の計算.前処理 $O(N \log N)$, 空間 $O(N \log N)$, クエリ $O(1)$.
7+
8+
## 使用方法
9+
10+
```cpp
11+
TreeLCA tree(N);
12+
for (int e = 0; e < N - 1; e++) {
13+
int u, v;
14+
cin >> u >> v;
15+
tree.add_edge(u, v);
16+
}
17+
18+
cout << tree.lca(a, b) << '\n'; // (a, b) の最長共通祖先
19+
cout << tree.path_length(a, b) << '\n'; // 2 頂点 a, b の距離
20+
```
21+
22+
## 問題例
23+
24+
- [Library Checker: Lowest Common Ancestor](https://judge.yosupo.jp/problem/lca)

tree/lowest_common_ancestor.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#include <vector>
44

55
// CUT begin
6-
// lowest common ancestor (LCA) class for undirected weighted tree
7-
// 無向重み付きグラフの最小共通祖先
8-
// https://yukicoder.me/submissions/392383
6+
// lowest common ancestor (LCA) for undirected weighted tree
97
template <typename T> struct UndirectedWeightedTree {
108
int INVALID = -1;
119
int V, lgV;

tree/lowest_common_ancestor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Lowest common ancestor (最小共通祖先)
33
documentation_of: ./lowest_common_ancestor.hpp
44
---
55

6-
根を固定した木に対する 2 頂点の最小共通祖先,および 2 頂点間の距離の計算.前処理 $O(N \log N)$, クエリ $O(\log N)$.
6+
根を固定した木に対する 2 頂点の最小共通祖先,および 2 頂点間の距離,$k$ 番目の親の計算.前処理 $O(N \log N)$, クエリ $O(\log N)$.
77

88
## 使用方法
99

0 commit comments

Comments
 (0)