LCA
重点例题 – 继续学习之前,请先尽力独立解决这道题!
View Internal Solution重点例题 – 继续学习之前,请先尽力独立解决这道题!
教程
| Resources | |||||
|---|---|---|---|---|---|
| CPH | |||||
| cp-algo | |||||
实现
| Resources | |||||
|---|---|---|---|---|---|
| Benq | |||||
import java.io.*;import java.util.*;public class LCA {public static int[] euler_tour, tin;public static int timer, size, N;public static ArrayList<Integer> g[];// Segtree codepublic static final int maxsize = (int)1e7; // limit for array size
稀疏表
上面的代码用 时间预处理,并能在 时间内完成 LCA 查询。 如果把计算最小值的线段树替换为稀疏表,就可以用 时间预处理,并在 时间内查询。
重点例题 – 继续学习之前,请先尽力独立解决这道题!
下面给出稀疏表的示例实现,以及用它回答 LCA 查询的代码。建表时间为 ,查询时间为 。
#include <bits/stdc++.h>using namespace std;template <typename T> class SparseTable {private:int n, log2dist;vector<vector<T>> st;public:SparseTable(const vector<T> &v) {
资源
| Resources | |||||
|---|---|---|---|---|---|
| CPH | 图示 | ||||
| PAPS | 代码 | ||||
| cp-algo | |||||
Optional: 更快的预处理
实现
| Resources | |||||
|---|---|---|---|---|---|
| Benq | |||||
题目
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| Gold | Medium | Show TagsEuler Tour, LCA, PURS | ||||
| Gold | Medium | Show TagsEuler Tour, LCA | ||||
| AC | Medium | Show TagsEuler Tour, LCA, PURS | ||||
| DMOPC | Hard | Show TagsEuler Tour, LCA, PURS | ||||