poj1330Nearest Common Ancestors(LCA小结)】的更多相关文章

题目请戳这里 题目大意:意如其名. 题目分析:本题只有一个查询,所以可以各种乱搞过去. 不过对于菜鸟而言,还是老老实实练习一下LCA算法. LCA有很多经典的算法.按工作方式分在线和离线2种. tarjan算法是经典的离线算法.这篇博客讲的太好懂了,我也不好意思班门弄斧,具体戳进去看看就会明白.重点是那个插图,一看秒懂. 在线算法主要有倍增算法和转RMQ算法. 另外LCA还有2种更为高效的O(n)-O(1)算法.一种请戳这里,另一种其实就是先将LCA转化成RMQ,再利用笛卡尔树O(n)预处理,O…
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节点编号. LCA裸题,用倍增思想. 代码总览 #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #define nmax 80520 #define demen…
题目链接:http://poj.org/problem?id=1330 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. N…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19728   Accepted: 10460 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:    In the figure, e…
pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA一发.然后N个顶点给出了N-1有向边,求一对点之间的最近公共祖先 思路: 裸的离线tarjan Lca即可,但注意是有向边,需要先找出根节点,数组标记.其次要注意前向星存的时候只存一条边即可 代码: #include <iostream> #include <string.h> usi…
Nearest Common Ancestors 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.…
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 ancestor of…
LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好的处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中,这样u结点和所有u的子树中的结点的最近公共祖先就是u了,u和还未遍历的所有u的兄弟结点及子树中的最近公共祖先就是u的父亲结点.以此类推..这样我们在对树深度遍历的时候就很自然的将树中的结点分成若干的集合,两个集合中…
题目连接  http://poj.org/problem?id=1330 就是构建一棵树,然后问你两个节点之间最近的公共父节点是谁? 代码: /*Source Code Problem: 1330 User: huifeidmeng Memory: 1232K Time: 63MS Language: C++ Result: Accepted Source Code */ #include<iostream> #include<vector> #include<cstdio&…
LCA离线算法 它需要一次输入所有的询问,然后有根节点开始进行深度优先遍历(DFS),在深度优先遍历的过程中,进行并查集的操作,同时查询询问,返回结果. 题意: 求A ,B两点的最近公共祖先 分析: dfs+并查集 // File Name: 1330.cpp // Author: Zlbing // Created Time: 2013年08月18日 星期日 16时11分18秒 #include<iostream> #include<string> #include<alg…