POJ 1330 

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 node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y.

Write a program that finds the nearest common ancestor of two distinct nodes in a tree.

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.

Output

Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.

Sample Input

2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5

Sample Output

4
3
最近在复习一些暑假集训时候学到的一些数据结构,这次的主题是LCA(最近公共祖先),找出一棵树里边任意两个节点的最近公共祖先节点(这个称呼不太科学?),这是一道全裸的LCA的题目,有两种解决思路
方法一:对于每次查询的两个节点,先让两个节点上升到同一个深度的地方,然后两个节点在同时上升,直到两个节点相遇为止,相遇的点即为最近公共祖先。
方法二:先让某个节点一直往上走一直走到根节点,并开一个数组记录这个路径,让后再让另一个节点往上走,直到与前一个节点产生的路径相交为止,那么这个交点也是两个节点的最近公共祖先啦~ 方法1的AC代码:
 /*********************************
Author: jusonalien
Email : jusonalien@qq.com
school: South China Normal University
Origin:
*********************************/
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = ;
vector<int>G[maxn];
int depth[maxn],father[maxn];
int root,n;
void dfs(int v,int p,int d){//通过dfs构造出一棵树,并且记录每个节点的深度,这个很重要!
depth[v] = d;
for(int i = ;i < G[v].size();++i){
if(G[v][i] != p) dfs(G[v][i],v,d+);
}
return ;
}
int lca(int u,int v){
while(depth[u] > depth[v]) u = father[u];
while(depth[v] > depth[u]) v = father[v];
while(u != v){
u = father[u];
v = father[v];
}
return u;
}
void init(){
memset(depth,,sizeof(depth));
memset(father,-,sizeof(father));
for(int i = ;i <= n;++i) G[i].clear();
}
void print(){//调试代码
for(int i = ;i <= n;++i) printf("%02d ",father[i]);
puts("");
for(int i = ;i <= n;++i) printf("%02d ",depth[i]);
puts("");
}
int main(){
int cas;
int a,b;
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
init();
for(int i = ;i < n;++i){
scanf("%d%d",&a,&b);
father[b] = a;
G[a].push_back(b);
}
for(int i = ;i <= n;++i)
if(father[i] == -){
root = i;break;
}
dfs(root,-,);
//print();
scanf("%d%d",&a,&b);
printf("%d\n",lca(a,b));
}
return ;
}

 方法2的AC代码:

 #include <cstdio>
#include <cstring>
using namespace std;
int const maxn = +;
int fa[maxn];
bool vis[maxn];
int n;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int u,v;
scanf("%d",&n);
memset(vis,,sizeof(vis));
memset(fa,,sizeof(fa));
for(int i=;i<n;++i)
{
scanf("%d%d",&u,&v);
fa[v]=u;
}
scanf("%d%d",&u,&v);
do
{
vis[u]=true;
u=fa[u];
}while(u!=);
do
{
if(vis[v])
{
printf("%d\n",v);
break;
}
v=fa[v];
}while(v!=);
}
return ;
}

个人觉得,方法1对于同一棵树上的大规模查询的效率要比方法2要高,并且当查询的节点大都在树的底层的时候,方法2会产生很多不必要的查询,也会产生较多的浪费(并且个人觉得方法1的代码更加优美?

Ps:这里有一份很不错的关于RMQ和LCA的学习资料介绍,请猛戳此处 选自农夫三拳。

POJ 1330 LCA裸题~的更多相关文章

  1. JZOJ5883【NOIP2018模拟A组9.25】到不了——动态LCA裸题

    题目描述 Description wy 和 wjk 是好朋友. 今天他们在一起聊天,突然聊到了以前一起唱过的<到不了>. "说到到不了,我给你讲一个故事吧." &quo ...

  2. poj 1330 LCA

    #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...

  3. poj 1330 LCA (倍增+离线Tarjan)

    /* 先来个倍增 */ #include<iostream> #include<cstring> #include<cstdio> #define maxn 100 ...

  4. POJ 3264 RMQ裸题

    POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...

  5. poj 1330 LCA最近公共祖先

    今天学LCA,先照一个模板学习代码,给一个离线算法,主要方法是并查集加上递归思想. 再搞,第一个离线算法是比较常用了,基本离线都用这种方法了,复杂度O(n+q).通过递归思想和并查集来寻找最近公共祖先 ...

  6. POJ 1330 LCA最近公共祖先 离线tarjan算法

    题意要求一棵树上,两个点的最近公共祖先 即LCA 现学了一下LCA-Tarjan算法,还挺好理解的,这是个离线的算法,先把询问存贮起来,在一遍dfs过程中,找到了对应的询问点,即可输出 原理用了并查集 ...

  7. POJ.1330 Nearest Common Ancestors (LCA 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  8. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

  9. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

随机推荐

  1. UML系统建模学习

    什么是UML系统建模 UML系统建模是一种与面向对象软件开发密切相关的建模方法.通过建造模型可以验证建造事物的可行性.UML是一种统一建模语言,它的全称是(Unified Method Languag ...

  2. Xcode报referenced from错误的总结

    一.库文件丢失 如果提示的文件是库文件,比如说是sdk的文件,有可能是就是丢失,或者没有引用到该工程. 1.点击这个.a库,或者framework,看右边的target里面是否引用到了当前的targe ...

  3. 慕课笔记利用css进行布局【一列布局】

    <html> <head> <title>一列布局</title> <style type="text/css"> bo ...

  4. 可并堆试水--BZOJ1367: [Baltic2004]sequence

    n<=1e6个数,把他们修改成递增序列需把每个数增加或减少的总量最小是多少? 方法一:可以证明最后修改的每个数一定是原序列中的数!于是$n^2$DP(逃) 方法二:把$A_i$改成$A_i-i$ ...

  5. Linux下C编程入门(7)

    Linux下项目同步工具介绍git和github 一.远程仓库工具github 1. 一.本地操作工具git 1.

  6. vim fulerformat的设置

    在vim中设置选项,有注释很容易明白: set laststatus=1 "2总显示最后一个窗口的状态行,1窗口多于一个时显示最后一个窗口的状态行,0不显示最后一个窗口的状态行 fulerf ...

  7. C#高级编程第9版 第一章 .NET体系结构 读后笔记

    .NET的CLR把源代码编译为IL,然后又把IL编译为平台专用代码. IL总是即时编译的,这一点的理解上虽然明白.当用户操作C#开发的软件时,应该是操作已经编译好的程序.那么此时安装在客户机上的程序是 ...

  8. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  9. Codeforces 651D Image Preview【二分+枚举】

    题意: 若干张照片,从头开始可以向左右两边读,已经读过的不需要再读,有的照片需要翻转,给定读.滑动和翻转消耗的时间,求在给定时间内最多能读多少页? 分析: 首先明确,只横跨一次,即先一直读一边然后再一 ...

  10. JSP页面不支持EL表达式的解决方法

    JSP页面不支持EL表达式的问题就出在新建项目时web.xml的声明上. web.xml声明部分一般分为如下版本的xsd: web-app_2_2.xsd web-app_2_3.xsd web-ap ...