POJ 1986(LCA and RMQ) 题意:给定一棵树,求任意两点之间的距离。 思路:由于树的特殊性,所以任意两点之间的路径是唯一的。u到v的距离等于dis(u) + dis(v) - 2 * dis(lca(u, v)); 其中dis(u)表示u到根节点的距离。 RMQ求LCA,过程如下,摘自http://dongxicheng.org/structure/lca-rmq/ 在线算法DFS+...
#include<cstdio> #include<cstring> using namespace std; const int maxn=80010; int fa[maxn],head[maxn],qhead[maxn],dis[maxn]; bool vis[maxn]; int id,iq; struct Node{//图中的边E和查询QE用统一结构体 int to; int next; int lca;//图中lca表示u、v的边权,查询中lca代表查询u、...
vector<Query>link[maxn];structEdge{intnext,to,length; }edge[maxn*10];//POJ1984上面说边数大小最多40000,由于双向边,我开了80000+,结果RE,后来开了10倍,才AC。。。voidadd(intu,intv,intl){ edge[tot].next=head[u]; edge[tot].to=v; edge[tot].length=l; head[u]=tot++; edge[tot].ne...
1 LCA指的是一棵有根树上两个点的最近公共祖先,或者指的是图上两个点的最短路径。 2 这一题的边数最大40000,还有k(k<=10000)次询问,如果利用最短路的算法肯定TLE。所以这个时候我们选择LCA,假设dis[x]是x到根节点的路径长度,那么(i , j)两点的最短路径为dis[i]+dis[j]-2*dis[LCA(i , j)];LC...
POJ 1986 Distance Queries LCA距离:快速查询树中任意两个节点间的最短距离。 4.3成为图论大师之路 LCA 也就是两个节点到最近公共祖先的距离之和,求出每个节点到根节点的距离之后,uv两点间距离=d[u]+d[v]-2*d[lca]。 LCA可以用RMQ快速实现: 对于涉及有根树的问题,将树转为从根DFS标号后得到的序列处理的...
POJ - 1986 Distance Queries(LCA离线),题目大意:给出N个点,M条边,Q个询问,询问的问题是这两个点之间的距离解题思路:
POJ 1986——Distance Queries 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 problem consists of the same input as in "...
最近公共祖先:LCA及其用倍增实现 +POJ1986 2016-01-28 13:51 −... sllr15 0 6899 最近公共祖先 LCA 倍增算法 2016-07-09 12:29 −倍增算法可以在线求树上两个点的LCA,时间复杂度为nlogn 预处理:通过dfs遍历,记录每个节点到根节点的距离dist[u],深度d[u] init()求出树上每个节点u的2^i祖先p[u...
对Nuculoidearesupina Guo,1985,PseudonuculanazhaoiPojeta,Zhangand Yang,1986和Skyphoconcha beichuanensis Liu and Gu,1988这3个种进行比较后,确定了它们的同物异名关系,确认船形蛤属(Skyphoconcha Liu and Gu)是假似栗蛤属(Pseudonuculana Pojeta,Zhang and Yang)的次异名.澄清了此属的韧带性质,并对其属征...
POJ 1986 LCA,tarjan实现 Distance Queries Time Limit:2000MSMemory Limit:30000K Total Submissions:4010Accepted:1418 Case Time Limit:1000MS 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 ...