![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LCA
穷源溯流
路很长,尽管走便是。
展开
-
洛谷 P3398 仓鼠找sugar(LCA)
const int N=1e5+5; int n,m,_; int i,j,k; int a[N]; vector<int> G[N];int dep[N],fa[N],sz[N],son[N];void dfs1(int u,int f){ dep[u]=dep[f]+1; fa[u]=f; sz[u]=1; for(int i=0;i<G[u].size();i++){ int v=G[u..原创 2021-05-17 14:30:30 · 274 阅读 · 0 评论 -
Codeforces Round #362 (Div. 1) A. Lorenzo Von Matterhorn(LCA)
NoteIn the example testcase:Here are the intersections used:Intersections on the path are3,1,2and4. Intersections on the path are4,2and1. Intersections on the path are only3and6. Intersections on the path are4,2,1and3. Pas...原创 2021-03-13 08:33:33 · 175 阅读 · 0 评论 -
洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树剖+lca)
如果有两个点,那么最短的位置一定是两点中的 lca,此时有三个点,我们观察 6 3 1 这组询问,如上图所示:最后的答案为 2,也就是 3 6 的 lca,不妨可以猜测,最后的位置一定是在某两点的 lca 处,所以枚举即可代码并没有采用倍增,而是采用树剖来处理 lcaconst int N=5e5+5; int n,m; int i,j,k; int a[N]; vector<int> G[N];int dep[N],fa...原创 2021-02-27 10:10:15 · 234 阅读 · 0 评论 -
1552:点的距离(倍增求 lca)
const int lgN=20+3;const int N=2e5+5; int n,m,t; int i,j,k; //int a[N]; vector<int> G[N]; int dep[N]; int f[N][lgN]; void dfs(int u,int fa){ dep[u]=dep[fa]+1; for(int i=1;i<lgN;i++){ f[u][i]=f[f...原创 2020-09-26 09:37:58 · 442 阅读 · 0 评论