题目:

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

Source

方法:

LCA模板

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
const int N=;
vector<int>p[N];
bool visit[N];
int deep[N],g[N][];
int t,a,b,n; inline void clear()
{
memset(g,,sizeof(g));
memset(p,,sizeof(p));
memset(deep,,sizeof(deep));
memset(visit,false,sizeof(visit));
} inline void dfs(int u)
{
for(int i=;i<p[u].size();i++)
{
int v=p[u][i];
if(g[u][]==v) continue;
g[v][]=u;
deep[v]=deep[u]+;
dfs(v);
}
} inline int lca(int a,int b)
{
int i;
if(deep[a]<deep[b]) swap(a,b);
for(i=;(<<i)<=deep[a];i++);
i--;
for(int j=i;j>=;j--)
{
if(deep[a]-(<<j)>=deep[b])
a=g[a][j];
}
if(a==b) return a;
for(i=;i>=;i--)
{
if(g[b][i]!=g[a][i])
{
b=g[b][i];
a=g[a][i];
}
}
return g[a][];
} int main()
{
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
scanf("%d",&t);
for(int i=;i<=t;i++)
{
clear();
int u,v;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
p[u].push_back(v);
visit[v]=true;
}
for(int i=;i<=n;i++)
{
if(visit[i]==false)
{
dfs(i);
break;
}
}
for(int i=;i<;i++)
for(int j=;j<=n;j++)
g[j][i]=g[g[j][i-]][i-];
scanf("%d%d",&a,&b);
cout<<lca(a,b)<<endl;
}
return ;
}

算法复习——LCA模板(POJ1330)的更多相关文章

  1. 算法复习——网络流模板(ssoj)

    题目: 题目描述 有 n(0<n<=1000)个点,m(0<m<=1000)条边,每条边有个流量 h(0<=h<35000),求从点 start 到点 end 的最 ...

  2. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. 【图论算法】LCA最近公共祖先问题

    LCA模板题https://www.luogu.com.cn/problem/P3379题意理解 对于有根树T的两个结点u.v,最近公共祖先LCA(u,v)表示一个结点x,满足x是u.v的祖先且x的深 ...

  4. LCA模板

    /*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca ...

  5. 倍增求lca模板

    倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> ...

  6. STL算法与树结构模板

    STL算法 STL 算法是一些模板函数,提供了相当多的有用算法和操作,从简单如for_each(遍历)到复杂如stable_sort(稳定排序),头文件是:#include <algorithm ...

  7. C#冒泡算法复习

    C#冒泡算法复习 冒泡算法的意思:每一趟找到一个最小或最大的数放到最后面,比较总数的n-1次(因为比较是2个双双比较的) 第一层循环表示进行比较的次数,总共要比较(数的)-1次 (因为比较是2个双双比 ...

  8. tarjan算法求LCA

    tarjan算法求LCA LCA(Least Common Ancestors)的意思是最近公共祖先,即在一棵树中,找出两节点最近的公共祖先. 这里我们使用tarjan算法离线算法解决这个问题. 离线 ...

  9. HDU 2586——How far away ?——————【LCA模板题】

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现

    1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...

  2. 国庆集训 || Wannafly Day1

    网址:https://www.nowcoder.com/acm/contest/201#question A.签到 手速石头剪刀布 #include <cstdio> #include & ...

  3. CeontOS6.5安装php环境

    港湾云主机重装操作系统之后xshell无法连接:重启ssh:# service sshd restart -bash: vim: command not found:输入 rpm -qa|grep v ...

  4. mysql dump 参数大全

    Mysqldump参数大全   摘自:https://www.cnblogs.com/qq78292959/p/3637135.html 参数 参数说明 --all-databases  , -A 导 ...

  5. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  6. JdbcTemplate类对sql的操作使用

    <!--方式一: dbcp 数据源配置,在测试环境使用单连接 --> <bean id="dataSource" class="org.apache.c ...

  7. shell脚本,一个字符一个字符输出。

    [root@localhost wyb]# cat file abc def abc 789de f567 [root@localhost wyb]# cat fffile.sh #!/bin/bas ...

  8. 使用xcode workspace 多个project协同工作

    一般的某个应用单独新建一个 project 就可以了,然后把所有的程序文件都放在里面,这个可以满足大部分普通的需求,但是有时候,项目有可能要使用其他的项目文件,或者引入其他的静态库文件,这个时候 wo ...

  9. angular-file-upload 在IE下使用的坑

    如果在控件配置里面设置了queueLimit属性为1,就是队列文件个数为1,并且在<input>标签设置里multiple属性. 在IE浏览器上传附件的时候,浏览器会报错“SCRIPT50 ...

  10. UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索

    UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...