POJ1330Nearest Common Ancestors】的更多相关文章

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&…
http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> #include<string> #include<set> #include<map> con…
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…
题目请戳这里 题目大意:意如其名. 题目分析:本题只有一个查询,所以可以各种乱搞过去. 不过对于菜鸟而言,还是老老实实练习一下LCA算法. LCA有很多经典的算法.按工作方式分在线和离线2种. tarjan算法是经典的离线算法.这篇博客讲的太好懂了,我也不好意思班门弄斧,具体戳进去看看就会明白.重点是那个插图,一看秒懂. 在线算法主要有倍增算法和转RMQ算法. 另外LCA还有2种更为高效的O(n)-O(1)算法.一种请戳这里,另一种其实就是先将LCA转化成RMQ,再利用笛卡尔树O(n)预处理,O…
去博客园看该题解 题意 第一行输入T,有T组数据. 对于每组数据,给出一棵树,先输入n,然后n-1行,每行两个数a,b,表示a是b的父亲:第n行输入两个数A,B表示询问A和B的最近公共祖先. 题解 LCA模板题. 参见LCA学习笔记 LCA倍增算法&POJ1330标程 #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cm…
用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改了改输入输出.这个的难度比那个低. #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; ; int father[MAXN],a…
[知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始时并不需要已经知道所有的输入. [倍增-在线算法] 水题……这道题范围卡得很紧…数组少了一位就会WA. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #incl…
传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Accepted: 13734 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:    In the figur…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accepted: 14052 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, eac…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14698   Accepted: 7839 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each…
  Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Accepted: 12779 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, e…
传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Accepted: 5549 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines…
Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accepted: 4944 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the…
题目链接: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 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩子,vector<int> v[M]写在主函数里面,放在外面超时. 代码: #include<iostream> #include<vector> #include<cstdio> #include<cstdlib> #include<cstr…
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…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accepted: 9608 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each…
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…
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…
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu Submit Status Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In t…
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest co…
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) 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…
pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA一发.然后N个顶点给出了N-1有向边,求一对点之间的最近公共祖先 思路: 裸的离线tarjan Lca即可,但注意是有向边,需要先找出根节点,数组标记.其次要注意前向星存的时候只存一条边即可 代码: #include <iostream> #include <string.h> usi…
Cloesest Common Ancestors 题目大意:给出一个n个节点的树,m组询问求两点LCA. 注释:n<=900. 想法:这题一看,我去,这不傻题吗?一看读入方式,完了,懵逼了... ...这题是考读入啊一大堆乱七八糟的东西,真正有用的只有里面的数... ...然后,我学了两个比较有用的东西. 1.scanf("%*d");其中,*表示读完就删,所以可以用来处理本题. 2.强大的nc+read(),在此不做介绍. 总之,初学LCA,这是另一道朴素的LCA,用桶记录即…
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 node y if no…
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 node y if n…
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.…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30147   Accepted: 15413 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, eac…
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…