LCA【模板】】的更多相关文章

/*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca::dfs中的your code处理每次查询 //复杂度O(M+Q) //表示图的邻接表 #define N 40100 #define MAX_Q 200 struct node { int to,next,w; }edge[*N]; int pre[N],cnt; int ans[MAX_Q];…
倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> #include<cmath> #include<cstring> using namespace std; int t,n,cnt,m; int x,y; ][],p,root; ]; ]; ]; ; struct node { int next,to; }e[*]; inline…
How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9359    Accepted Submission(s): 3285 Problem Description There are n houses in the village and some bidirectional roads connecting…
题目: Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancesto…
How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25408    Accepted Submission(s): 10111 Problem Description There are n houses in the village and some bidirectional roads connectin…
题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n),每次查询为logn,因此总复杂度为:O(2*n+m*logn). 代码: #include<cstdio> #include<cstring> using namespace std; ; struct node{ int v,next; }edge[*maxn]; int n,m,…
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果访问过的话,那么它的最近公共祖先就是当前节点祖先. 下面是tarjan离线模板: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ; struct Edge { i…
题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例如以下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 40010; str…
以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵树上距离最近的公共祖先节点. 所以LCA主要是用来处理当两个点仅有唯一一条确定的最短路径时的路径. 有人可能会问:那他本身或者其父亲节点是否可以作为祖先节点呢? 答案是肯定的,很简单,按照人的亲戚观念…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house…
LCA 有几种经典的求取方法.这里只给出模板,至于原理我完全不懂. 1.RMQ转LCA.复杂度O(n+nlog2n+m) 大致就是 DFS求出欧拉序 => 对欧拉序做ST表 => LCA(u, v) 即为 u.v 最先出现在欧拉序中的编号之间的最小值. 因为 LCA 的子树中必定有一个节点是 u,一个是 v,而且必定在两个节点到根节点的唯一路径上. 例如有欧拉序列 1 2 1 3 4 3 1 则 LCA(2, 3) == 1 .首次出现 2 的下标是 2.首次出现 3 的下标是 4.则 LCA…
题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t,f ,求路径 (s,t) (s,f) (t,f) 三者的最大公共结点数 对于(t,f) (s,f)的公共结点数,有 懒得细想了,暴力对s,t,f生成排列(反正一共仨数,最多也就3!=6个排列),求各种情况下的最大ans #include<bits/stdc++.h> using namespac…
题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhead[maxm]. 输出用%I64d,不要用%lld. C++ RE后 尝试用 G++交. 代码: struct node{ int to,w,next,lca; }; int const maxn = 40005; int const maxm = 205; int fa[maxn]; int he…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 设dist[i]为i到根0的链和,求法(Dfs过程中dist[v]=dist[u]+e[i].w) 对于树中任意两点形成的链,可以通过LCA最近公共祖先剖分. 比如2->3,就可以经过LCA点1:  2->1->3 链和=dist[u]+dist[v]-2*dist[LCA[u,v]] (…
在线LCA 如求A,B两点的LCA,先计算出各个结点的深度d[],然后,通过递推公式求出各个结点的2次方倍的祖先p[],假设d[A] > d[B],则找到d[p[A][i]] == d[B]也就是A的某一祖先与B深度相同,然后,u = p[A][i],通过p[u][i] 与p[B][i]比较找出LCA(巧妙的利用二进制). (p[a][b] 表示与a的距离为2^b的祖先,则p[a][0]表示为a的父亲.如 a->b->c->d->e,a为根, 则p[e][2] 为a) 递推公…
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Status][Discuss] Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=1…
HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like th…
人丑常数大,总是卡在1000多ms... #include <cstdio> #include <cstring> #include <iostream> #define maxn 5000005 #define jump_max 19 struct Edge{ int from,to; }; Edge edge[maxn]; int n,m,s,x,y,a,b; int dep[maxn]; ]; int head[maxn]; ; inline void swap_…
关于LCA: LCA 指树上两点的公共祖先. 如何 “暴力” 找两点的 LCA : 可以先 DFS 一遍求出每个点的 dep (深度).然后从深度大的点先往上跳,跳到与另一个点相同的深度,如果还没有到达相同的,就两个点一起往上跳,直到达到相同的点,那么,这个点就是两点的 LCA . 关于倍增法求 LCA : 其实就是一个经过优化的暴力算法.让两个点一次向上跳多步来优化时间. 如何实现倍增求 LCA : 设 f [ u ][ k ] 表示 u 的 2k 辈祖先,即从 u 向根节点走 2k 步到达的…
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this prob…
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cma…
在一棵树上 求2个点的最短距离.那么首先利用LCA找到2个点的近期公共祖先 公式:ans = dis(x) + dis(y) - 2 * dis(lca(x,y)) 这里的dis(x)指的上x距离根节点的距离 注意一些细节方面,比方数组的越界问题: #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; typedef long…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后用dp得出每个节点向上跳2^k步到达的节点. 那么对于一个查询u,v,不妨设depth[u]>=depth[v],先让u向上跳depth[u]-depth[v]步,跳的方法就是直接用数字的二进制表示跳. 然后现在u和v都在同一深度上了,再二分找向上共同的祖先,就可以二分出lca了.复杂度nlogn预…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意: 给出一棵 n 个节点的带边权的树, 有 m 个形如 x y 的询问, 要求输出所有 x, y节点之间的最短距离. 思路: dis[i] 存储 i 节点到根节点的最短距离, lca 为 x, y 的最近公共祖先, 那么 x, y 之间的最短距离为: dis[x] + dis[y] - 2 * dis[lca] . 解法1: tarjan离线算法 关于该算法 Tarjan(u)//marg…
第一题LCA,代码参考自:Ice_Crazy 思路: 这个最短路算法是想都别想了,可以看出这幅图就是树嘛,那么对于查询就是求树上两个结点最短距离. 这里就是利用LCA的tarjan离线算法. 算法的大致流程: 对于每一点u, ①  :建立以u为代表元素的集合. ②  :遍历与u相连的结点v,如果没有访问过,对与v使用Tarjan-LCA算法,结束后,将v的集合并入u的集合. ③  :对于与u相关的询问(u,v),如果v被访问过,则结果就是v所在集合的代表. 在这里还需要算距离,需要深度:dis<…
How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8712    Accepted Submission(s): 3047 Problem Description There are n houses in the village and some bidirectional roads connecting…
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he wan…
void dfs(int x,int root){//预处理fa和dep数组 fa[x][0]=root; dep[x]=dep[root]+1; for(int i=1;(1<<i)<dep[x];i++) fa[x][i]=fa[fa[x][i-1]][i-1]; for(int i=0;i<ve[x].size();i++)dfs(ve[x][i],x); } int lca(int a,int b){//计算两个节点的最近公共祖先 if(dep[a]>dep[b])s…
Dfs(int rt){ f[][rt]; ;k<=;k++) f[k][rt]=f[k-][f[k-][rt]]; } int LCA(int x,int y){ if(Dp[x]<Dp[y])swap(x,y); int res=Dp[x]-Dp[y]; ;k>=;k--) <<k)<=res)x=f[k][x],res-=<<k; if(x==y)return x; ;k>=;k--){ if(f[k][x]!=f[k][y]) x=f[k][x…
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最近距离: 思路:设d[maxn]是当前点到根结点的距离,则答案就是 d[a]-d[lca(a,b)]+d[b]-d[lca(a,b)]: 接下来介绍LCA(Least Common Ancestors) 即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 常见解法一般有三种 这里讲…